1.2.306. Could Use str_repeat()¶
Use str_repeat() or str_pad() instead of making a loop.
Making a loop to repeat the same concatenation is actually much longer than using str_repeat(). As soon as the loop repeats more than twice, str_repeat() is much faster. With arrays of 30, the difference is significant, though the whole operation is short by itself.
<?php
// This adds 7 'e' to $x
$x .= str_repeat('e', 7);
// This is the same as above,
for($a = 3; $a < 10; ++$a) {
$x .= 'e';
}
// here, $default must contains 7 elements to be equivalent to the previous code
foreach($default as $c) {
$x .= 'e';
}
?>
1.2.306.1. Suggestions¶
Use strrepeat() whenever possible
1.2.306.2. Specs¶
Short name |
Structures/CouldUseStrrepeat |
Rulesets |
|
Exakat since |
0.11.0 |
PHP Version |
All |
Severity |
Minor |
Time To Fix |
Slow (1 hour) |
Precision |
Very high |
Features |
string |
Examples |
|
Available in |