php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #1323 strpos and 0th indexed strings
Submitted: 1999-04-16 14:09 UTC Modified: 1999-04-16 16:03 UTC
From: maarnold at gte dot net Assigned:
Status: Closed Package: Misbehaving function
PHP Version: 3.0.5 OS: linux
Private report: No CVE-ID: None
 [1999-04-16 14:09 UTC] maarnold at gte dot net
strpos() returns false if needle is not found in haystack. false is 0. Strings are indexed starting at 0. How should the caller of strpos() that strpos() didn't find the character at the beginning of the string. For example,...

printf( "pos is %d<br>\n", strpos( "abcdefg", "z" ) );
printf( "result is %s<br>\n", ( strpos( "abcdefg", "z" ) ) ? "true" : "false" );
printf( "pos is %d<br>\n", strpos( "abcdefg", "a" ) );
printf( "result is %s<br>\n", ( strpos( "abcdefg", "a" ) ) ? "true" : "false" );

...displays...

pos is 0
result is false
pos is 0
result is false

Most languages that have 0th indexed strings have strpos() return -1 when needle is not found in haystack. Either that, or the language has a special value for false (such as NULL instead of 0).

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [1999-04-16 16:03 UTC] rasmus
Untrue.  false is "" which is different from a string index of 0.  You just have to check it correctly.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 08:01:28 2024 UTC