1.2.1170. Substring First

Always start by reducing a string before applying some transformation on it. The shorter string will be processed faster.

The gain produced here is greater with longer strings, or greater reductions. They may also be used in loops. This is a micro-optimisation when used on short strings and single string reductions.

This works with any reduction function instead of substr(), like trim(), iconv(), etc.

<?php

// fast version
$result = strtolower(substr($string, $offset, $length));

// slower version
$result = substr(strtolower($string), $offset, $length);
?>

1.2.1170.1. Suggestions

  • Always reduce the string first, then apply some transformation

1.2.1170.2. Specs

Short name

Performances/SubstrFirst

Rulesets

All, Changed Behavior, Performances, Suggestions, Top10

Exakat since

1.0.1

PHP Version

All

Severity

Minor

Time To Fix

Instant (5 mins)

Precision

Very high

Features

declare

Examples

SPIP, PrestaShop

Available in

Entreprise Edition, Exakat Cloud