1.2.1169. 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.1169.1. Suggestions

  • Use str_split()

1.2.1169.2. Specs

Short name

Performances/SubstrInLoops

Rulesets

All, Changed Behavior, Performances

Exakat since

2.5.2

PHP Version

All

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

Medium

Available in

Entreprise Edition, Exakat Cloud