Which method is preferred strstr or strpos?

I noticed that many developers use strstr and strpos to check for the existence of a substring. Is one of them preferable and why?

+75
php strpos strstr
Apr 28 2018-11-21T00:
source share
5 answers

From the online PHP manual :

If you only want to determine if a particular needle is in a haystack, use the faster and less intense memory function strpos() instead.

+116
Apr 28 '11 at 2:55 april
source share

Here are a few other answers (+ tests). I got to my question, which is almost the same (I did not understand yours when you ask).




In the meantime, I also made my own control test, which I performed 1,000,000 times for each corresponding function ( strstr() , strpos() , stristr() and stripos() ).
Here is 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 conclusion that 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 result ( strpos() wins 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 the winner is: strstr() :

 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 that it can really depend on “environmental circumstances” , which are sometimes difficult to influence, and can change the result of “microoptimization tasks” as follows if you just check if the string exists in another or not.

BUT I think that in most cases strpos() is a winner compared to strstr() .

I hope this test was useful for someone.

+33
Apr 28 '11 at 7:10
source share

Many developers use strpos for micro optimization purposes.

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

+6
Apr 28 2018-11-21T00:
source share

strpos () determines where a particular needle is located in the haystack. stristr () checks if the needle is in a haystack

for this strpos () consumes memory faster and less

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

0
Apr 28 2018-11-21T00:
source share

I prefer strstr() for readability and easy coding. strpos() !==false little confused.

-3
Dec 19 '14 at 16:35
source share



All Articles