They all return a positive index if the substring is found, or -1 if not.

Unfortunately PHP can sometimes return FALSE, or 0 (zero), or "" when it doesn't find the substring, which makes programming logic to handle this extremely difficult.

Known Workarounds: Several. Many developers have written new extensions to the String Class, standalone functions and more. Rather than claim to have the best solution ourselves, please send in your workaround using the comments below and we'll try to pick and highlight the best solutions from the submissions.

12 comments:

Do you really think that strongly described in manual behaviour of function is a bug?

And where did you find phrase 'Unfortunately PHP can sometimes return FALSE, or 0 (zero), or "" when it doesn't find the substring'?

As I understand, PHP will return 0 if source string starts with the substring you search. And if you use equal operator (==) instead of identical (===) you can made a wrong conclusion on occurrence of substring in a string.

Are you kidding me ?strpos returns an integer if the value is found and false otherwise.That's why in order to test if the needle isn't in the string you have to test it against false with === (explicit type comparison baby).

But it should be a normal thing for programmers to test the type of a return.Javascript programmers are used to do that too.

PHP works differently, why are you calling this a bug ? This is by design working properly.

There is good reason why it's returning FALSE and not -1. In some fonction like substr (http://www.php.net/substr) "-1" means last character, it doesn't mean an index that doesn't exist. To avoid any confusion it returns FALSE. Get inform before posting stuff like that.

Also I'd like to make you notice that even if it was returning -1, you would still have to test if it was founded or not. What you have to do is to test if it's FALSE instead of -1.

Also, most of people that thinks it's a bug argumentation are that most language native function return -1, but PHP doesn't. PHP doesn't do it like the other language, but that doesn't mean it's wrong. It's a totally wrong way of thinking to say that because all language do it in a way, that all language should do it the same way.

So no mather if it was returning -1 or FALSE you still have to do a test. It changes nothing to the way you would have done your script and it doesn't cause any problem to script in any circumstance. And it's a bug ?

This is being called a bug only because of the opinion of the author and his opinion is highly debatable.

Recent Updates

Feb. 7,2009 - Added a new Tag called Broken By Design. It is applied to all reported bugs that the browser vendor has indicated that they have no intention of fixing this bug any time soon and is therefore broken "By Design".

Feb. 7,2009 - Updated as many bug reports as possible for Internet Explorer 8 (IE8) to reflect the status of IE8 RC1 since according to the IE Blog the RTM release is coming soon. If you want to know, click to see what has been fixed?