0

Is it bad form to reuse the variable name when you're manipulating a string like the example below?

    <?php
    $string = "Jimmy <b>likes</b> red shoes";
    $string = strip_tags($string);
    $string = str_replace("red", "blue", $string);
    $string = strtoupper($string);
    echo $string;
    ?>

If it's a no-no, what is preferred? Should one try to make it into one line of code? Or use 4 different variable names?

I tried searching, but the only reference I could find was in regards to changing the variable from an array to a string or something like that.

3
  • I don't think this is what they mean by "don't reuse variable names". Just sayin'. Commented Dec 28, 2011 at 3:28
  • For me it's ok and it's exactly how I do this kind of statements. Commented Dec 28, 2011 at 3:29
  • Instead of thinking of it as "reusing the name" you could think of it as you're just modifying the same variable several times. And no, I'd say there's absolutely nothing wrong with it. Commented Dec 28, 2011 at 3:32

2 Answers 2

3

There's nothing wrong with it, as long as you don't need the original $string value again later. Once you've done $string = something($string), the original value is destroyed and replaced with the modified version.

In theory, you could simply chain the entire sequence together

echo strtoupper(str_replace('red', 'blue', strip_tags('Jimmy likes...')));

but that makes for unreadable and unmaintainable code.

Sign up to request clarification or add additional context in comments.

3 Comments

I remember one of my hurdles learning PHP was trying to read stuff like this from the inside out. OP's version is definitely much clearer.
@Marmartigan: OP's version is much clearer. But, from this article: code.google.com/speed/articles/optimizing-php.html : Don't copy variables for no reason.
@ZulkhaeryBasrul: "No reason"? The reason is readability and ease of maintenance. I can think of several other reasons but I'll leave it at that.
0

Variable names should be a short, precise description of the data stored therein. Consequently, re-using a variable name is fine, even preferable when there isn't a better name. On the flip side, a variable shouldn't be re-used in a misguided attempt at optimization:

for ($i=0; $i < n; ++$i) {
    ...
}
for ($map as $i => $value) {
    ...
}

The indices (values stored in $i) in the second loop better be integers, rather than re-using $i to store strings, because $i is almost universal code for an integer index. If someone insists on clearing space, they can use unset.

The real danger of variables is using them in broad scope (globals and large functions), where namespace collisions can cause data to be overwritten. In addition to preventing collisions, small functions can help reduce memory usage, as local variables will be more short lived.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.