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.
There’s nothing wrong with it, as long as you don’t need the original
$stringvalue 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
but that makes for unreadable and unmaintainable code.