Which method is preferred strstr or strpos? Which method is preferred strstr or strpos? php php

Which method is preferred strstr or strpos?


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.


Here 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:

<?phpfunction 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.39144707stristr():           3.65685797strpos() !== false:  2.39055395stripos() !== false: 3.54681897time_needed_stristr - time_needed_strstr: 1.2654109time_needed_stripos - time_needed_strpos: 1.15626502time_needed_strstr  - time_needed_strpos:  0.00089312time_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.39969015stristr():           3.60772395strpos() !== false:  2.38610101stripos() !== false: 3.34951186time_needed_stristr - time_needed_strstr: 1.2080338time_needed_stripos - time_needed_strpos: 0.96341085time_needed_strstr  - time_needed_strpos:  0.01358914time_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.35499191stristr():           3.60589004strpos() !== false:  2.37646604stripos() !== false: 3.51773095time_needed_stristr - time_needed_strstr: 1.25089812time_needed_stripos - time_needed_strpos: 1.14126492time_needed_strstr  - time_needed_strpos:  -0.02147412time_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.


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.