Which method is preferred strstr or strpos?

PhpStrposStrstr

Php Problem Overview


I noticed a lot of developers are using both strstr and strpos to check for a substring existence. Is one of them preferred and why ?

Php Solutions


Solution 1 - Php

From the PHP online manual:

> If you only want to determine if a > particular needle occurs within > haystack, use the faster and less > memory intensive function strpos() > instead.

Solution 2 - Php

https://stackoverflow.com/questions/5821483/what-is-the-fastest-way-to-find-the-occurrence-of-a-string-in-another-string/">Here</a> are some other answers (+benchmarks) I got to my question, which is almost the same (I didn't realize yours when asking).


In the meantime I also made my own benchmark test, which I ran 1000000 times for each relevant functions (strstr(), strpos(), stristr() and stripos()).
Here's the code:

<?php

function getmicrotime() {
	list($usec, $sec) = explode(" ", microtime());
	return ((float) $usec + (float) $sec);
}

$mystring = 'blahblahblah';  
$findme = 'bla';  

echo 'strstr & strpos TEST:<pre>';
$time_start = getmicrotime();
for($i=0; $i<1000000; $i++) strstr($mystring, $findme);
$time_needed_strstr = getmicrotime() - $time_start;
echo 'strstr():            ',
	round( $time_needed_strstr , 8 ). PHP_EOL;

$time_start = getmicrotime();
for($i=0; $i<1000000; $i++) stristr($mystring, $findme);
$time_needed_stristr = getmicrotime() - $time_start;
echo 'stristr():           ',
	round( $time_needed_stristr , 8 ) . PHP_EOL;

$time_start = getmicrotime();
for($i=0; $i<1000000; $i++) strpos($mystring, $findme) !== false;
$time_needed_strpos = getmicrotime() - $time_start;
echo 'strpos() !== false:  ',
	round( $time_needed_strpos , 8 ) . PHP_EOL;

$time_start = getmicrotime();
for($i=0; $i<1000000; $i++) stripos($mystring, $findme) !== false;
$time_needed_stripos = getmicrotime() - $time_start;
echo 'stripos() !== false: ',
	round( $time_needed_stripos , 8 ) . PHP_EOL;

echo PHP_EOL;

echo 'time_needed_stristr - time_needed_strstr: ',
	 round( $time_needed_stristr - $time_needed_strstr , 8) . PHP_EOL;
echo 'time_needed_stripos - time_needed_strpos: ',
	 round( $time_needed_stripos - $time_needed_strpos , 8) . PHP_EOL;

echo PHP_EOL;

echo 'time_needed_strstr  - time_needed_strpos:  ',
	 round( $time_needed_strstr - $time_needed_strpos , 8) . PHP_EOL;
echo 'time_needed_stristr - time_needed_stripos: ',
	 round( $time_needed_stristr - $time_needed_stripos , 8) . PHP_EOL;

echo '</pre>';

?>


And here is the first output, which shows that strpos() is the winner:

strstr & strpos TEST:
strstr():            2.39144707
stristr():           3.65685797
strpos() !== false:  2.39055395
stripos() !== false: 3.54681897

time_needed_stristr - time_needed_strstr: 1.2654109
time_needed_stripos - time_needed_strpos: 1.15626502

time_needed_strstr  - time_needed_strpos:  0.00089312
time_needed_stristr - time_needed_stripos: 0.110039	

The next one is similar to the first output (strpos() is the winner again):

strstr & strpos TEST:
strstr():            2.39969015
stristr():           3.60772395
strpos() !== false:  2.38610101
stripos() !== false: 3.34951186

time_needed_stristr - time_needed_strstr: 1.2080338
time_needed_stripos - time_needed_strpos: 0.96341085

time_needed_strstr  - time_needed_strpos:  0.01358914
time_needed_stristr - time_needed_stripos: 0.25821209

Below is another one, which is more interesting, because in this case, strstr() is the winner:

strstr & strpos TEST:
strstr():            2.35499191
stristr():           3.60589004
strpos() !== false:  2.37646604
stripos() !== false: 3.51773095

time_needed_stristr - time_needed_strstr: 1.25089812
time_needed_stripos - time_needed_strpos: 1.14126492

time_needed_strstr  - time_needed_strpos:  -0.02147412
time_needed_stristr - time_needed_stripos: 0.08815908

This means it can really depend on "environmental circumstances", which are sometimes hard to influence, and can change the result of "micro optimization tasks" like this, in case you are just checking whether a string exists in another one or not.

BUT I think in most cases, strpos() is the winner in comparison to strstr().

I hope this test was useful for someone.

Solution 3 - Php

Many developers use strpos for micro optimization purposes.

Using strstr also only works if the resulting string cannot be interpreted as false in boolean context.

Solution 4 - Php

strpos() detects where in the haystack a particular needle lies. stristr() tests whether the needle is anywhere in the haystack

therefor strpos() is faster and less memory consuming

a reason for strstr(): if your needle is at the beginning of a string, strpos returns 0 (so have to check it with === false)

Solution 5 - Php

I prefer strstr() for readability and easy coding.. strpos() !==false is confusing a bit..

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjohnlemonView Question on Stackoverflow
Solution 1 - PhpAlnitakView Answer on Stackoverflow
Solution 2 - PhpSk8erPeterView Answer on Stackoverflow
Solution 3 - PhpmarioView Answer on Stackoverflow
Solution 4 - PhpFlaskView Answer on Stackoverflow
Solution 5 - PhpT.ToduaView Answer on Stackoverflow