php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #3933 strpos doesn't find needle under certain conditions
Submitted: 2000-03-27 00:27 UTC Modified: 2000-03-27 01:29 UTC
From: wayne at pruvodce dot cz Assigned:
Status: Closed Package: Misbehaving function
PHP Version: 3.0.15 OS: Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: wayne at pruvodce dot cz
New email:
PHP Version: OS:

 

 [2000-03-27 00:27 UTC] wayne at pruvodce dot cz
<?
if(strpos("bleble","ble",0)==false) echo("strpos has a bug!(1)\n");
if(strpos("bleble","ble",-1)==false) echo("strpos has a bug!(2)\n");
if(strpos("bleble","ble")==false) echo("strpos has a bug!(3)\n");
if(strpos("blebla","ble")==false) echo("strpos has a bug!(4)\n");
if(strpos("blable","ble")!=false) echo("now it works, weird ...\n");
if(strpos("xbleble","ble",0)!=false) echo("now it works, weird ...\n");
?>

(My friend found a bug in my template engine ... i'm pretty sure that this is the problem)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2000-03-27 01:17 UTC] rasmus at cvs dot php dot net
This is not a bug.  strpos("bleble","ble") returns position 0 as it should.
The way you are doing your check is incorrect.  The condition 0 == false is true.  You want to check for an empty string here "" to check to see if the string was not found.  A 0 return is a match.  Please see the user notes in the online manual.
http://www.php.net/manual/function.strpos.php3
 [2000-03-27 01:24 UTC] wayne at pruvodce dot cz
OK, but manual says:
If needle is not found, returns false.
My check is correct (according to documentation).
I think it's better to return "-1" when needle isn't found.
 [2000-03-27 01:29 UTC] rasmus at cvs dot php dot net
It does return false when the needle is not found.  However 0 and false are
ambiguous and as such your check is wrong.  There is nothing incorrect in the manual, however it could be a bit clearer.
This is also one of the reasons the === operator was introduced in PHP 4.
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Sun Jun 28 18:00:02 2026 UTC