php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #15691 Returns from strpos()
Submitted: 2002-02-23 15:12 UTC Modified: 2002-02-24 10:23 UTC
From: mjohnwood at hotmail dot com Assigned:
Status: Closed Package: Feature/Change Request
PHP Version: 4.1.1 OS: Any
Private report: No CVE-ID: None
 [2002-02-23 15:12 UTC] mjohnwood at hotmail dot com
It would be boundlessly helpful if someone could make strpos(), etc. return a -1 instead of FALSE like C++/Java.  Returning a FALSE (something that casts to an integer of 0) doesn't seem to make much sense.  Even if it can be worked around with strpos() === FALSE, it would be a lot easier without having to rely upon it.  Any reason why it should be using FALSE instead of -1?

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-02-23 15:23 UTC] derick@php.net
Changing this behavior will break way too many scripts, so it's absolutely not a good idea to change this behavior.

Derick
 [2002-02-24 10:23 UTC] rasmus@php.net
I think returning -1 would be much worse.  That would mean that if(strpos($str,"foo")) would evaluate to true when "foo" does not exist in $str.  Bad idea.  
What has been suggested is to have it start at 1 instead of 0 to indicate a match in the first position.  That is a better idea, except it is inconsistent with the rest of PHP.  String indices start at 0 and often the result of a strpos() is passed to substr() in which case starting the strpos() return at 1 would really confuse people.
The way it currently is implemented is the most consistent and least confusing in my opinion.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Fri Jul 18 14:00:02 2025 UTC