Alex Rivera | Logout

Which method is preferred strstr or strpos ?

Asked 2011-04-28T14:53:36.667
90

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 ?

Edit
Report

1 Answer

0

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

Therefore 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 you have to check it with === false)

answered 2011-04-28T14:56:35.203

Your Answer