I want to know the difference here with the two codes: The first one is short but the same output came with the second code, I only see the design difference.
for ($i=5;$i>=1;$i–){
echo str_repeat(“*”, “$i”);
echo ‘ ’;
}
Didn’t say that - in this case, it’s two approaches to solve the same problem.
If all you want to do is repeat the same text X number of times, then str_repeat is the perfect vehicle to use. If, however, you would want to repeat the text X number of times, but change every 3rd character to a number and every 8th character to an exclamation point, and every 2nd letter out of every 10th group of 20 characters, then str_replace may not be the best approach. At that time, a separate loop would be more appropriate because it can give you more granular control.
Use the best tool (or block of code) for the right situation…