1.2.1646. strpos() Too Much¶
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 |
|
Exakat since |
1.2.8 |
PHP Version |
All |
Severity |
Minor |
Time To Fix |
Instant (5 mins) |
Precision |
High |
Examples |
|
Available in |