php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #7763 strrpos returns unclear value
Submitted: 2000-11-11 18:49 UTC Modified: 2000-11-11 22:56 UTC
From: timo at fi-web dot de Assigned:
Status: Closed Package: Documentation problem
PHP Version: 4.0.3pl1 OS: Linux
Private report: No CVE-ID: None
 [2000-11-11 18:49 UTC] timo at fi-web dot de
The following code doesn't work as expected:

$haystack = "/test";
$needle = "/";

$position = strrpos($haystack, $needle);

if ($position == false) { echo "Couldn't find $needle"; }

It seems that strrpos returns false, even if the position of the needle was 0 (It is the first position in the string, isn't it?). strrpos should return 0 if the needle was found in haystack at position 0 and FALSE if the needle wasn't found. Additionally, strrpos also returns 0 if the needle wasn't found in haystack. Maybe FALSE and 0 are defined as the same in that context? IMHO, 0 and FALSE should be defined different, or strrpos should NOT return a value (like unset) if the needle wasn't found in haystack.

Regards
Timo

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2000-11-11 19:06 UTC] torben@php.net
Not a bug. If the found string occurs at the first position
in the string, that position will be 0, as you noted. 0 is
a false value, so '0 == false' is true.

There are various ways around this. One is to use the 
PHP 4-only 'identical' operator, ===. This tests to make
sure that the operands are both equal and of the same type.

$haystack = "/test";
$needle = "/";
$position = strrpos($haystack, $needle);

if ($position === false) {
    echo "Not found";
} else {
    echo "Found at position $position";
}

Another is to do something like:

$position = strrpost("x$haystack", $needle);

...which would force any found strings to be at least 
at position 1.

Check out the manual pages on Type Juggling and Operators
for more information.

http://www.php.net/manual/language.types.type-juggling.php
http://www.php.net/manual/language.operators.comparison.php
 [2000-11-11 19:06 UTC] torben@php.net
S'pose I should close it now...:)
 [2000-11-11 19:07 UTC] timo at fi-web dot de
Actually, the function works as expected, but the documentation lacks the little comment about the three equal signs found in the documentation for strpos. An additional note in the predefined constants section about FALSE is defined as ZERO would be helpful, too. Unfortunately, I don't have CVS access so I can't do the changes myself.
 [2000-11-11 19:11 UTC] timo at fi-web dot de
Actually, the function works as expected, but the documentation lacks the little comment about the three equal signs found in the documentation for strpos. An additional note in the predefined constants section about FALSE is defined as ZERO would be helpful, too. Unfortunately, I don't have CVS access so I can't do the changes myself.
 [2000-11-11 22:56 UTC] sniper@php.net
Documentation fixed for strrpos()

--Jani
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Sun Jun 14 09:00:02 2026 UTC