1.2.1178. Substr() In Loops¶
Successive substr() calls may be replaced by a call to str_split().
It speeds up the processing, and allows the replacement of indefinite loops by a foreach() call.
This is a micro optimisation. It works better on longer strings.
<?php
$bits = str_split($string, 5);
foreach($bits as $bit) {
foo($bit);
}
$i = 0;
$s = strlen($string);
while($i < $s) {
// repeating calls to substr during the loop
foo(substr($string, $i * 5, 5));
$i += 5;
}
?>
1.2.1178.1. Connex PHP features¶
1.2.1178.1.1. Suggestions¶
Use str_split()
1.2.1178.1.2. Specs¶
Short name |
Performances/SubstrInLoops |
Rulesets |
|
Exakat since |
2.5.2 |
PHP Version |
All |
Severity |
Minor |
Time To Fix |
Quick (30 mins) |
Precision |
Medium |
Available in |