1.2.1646. strpos() Too Much

strpos() covers the whole string before reporting 0. If the expected string is expected be at the beginning, or a fixed place, it is more stable to use substr() for comparison.

The longer the haystack (the searched string), the more efficient is that trick. The string has to be 10k or more to have impact, unless it is in a loop. This applies to stripos() too.

<?php

// This always reads the same amount of string
if (substr($html, 0, 6) === '<html>') {

}

// When searching for a single character, checking with a known position ($string[$position]) is even faster
if ($html[0] === '<') {

}

// With strpos(), the best way is to search for something that exist, and use absence as worst case scenario
if (strpos($html, '<html>') > 0) {

} else {
    //
}

// When the search fails, the whole string has been read
if (strpos($html, '<html>') === 0) {

}

?>

1.2.1646.1. Suggestions

  • Check for presence, and not for absence

  • Use substr() and compare the extracted string

  • For single chars, try using the position in the string

1.2.1646.2. Specs

Short name

Performances/StrposTooMuch

Rulesets

All, Analyze, CE, CI-checks, Changed Behavior

Exakat since

1.2.8

PHP Version

All

Severity

Minor

Time To Fix

Instant (5 mins)

Precision

High

Examples

WordPress

Available in

Entreprise Edition, Community Edition, Exakat Cloud