php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #2682 Function strpos have a problem with pos 0 and false
Submitted: 1999-11-08 03:39 UTC Modified: 1999-12-15 14:58 UTC
From: rehr at supermall dot dk Assigned:
Status: Closed Package: Other
PHP Version: 4.0 Beta 2 OS: Linux Redhat 6.0
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: rehr at supermall dot dk
New email:
PHP Version: OS:

 

 [1999-11-08 03:39 UTC] rehr at supermall dot dk
<?
$haystack = "123456789";
$needle = "1";
$tmp = strpos($haystack,$needle);
if ($tmp)
{
	printf("Yes");
}
else
{
	printf("No");
}
?>

This prints no, because it returns 0 which is the same as false.

The documentation says:
If needle is not found, returns false. 

The problem if needle is at position 0 i haystack then it returns 0 which seems to be the same as false, therefore if needle is at position 0 
you get the same result as if it is not in haystack at all.

I don't know if this is a bug or just something that should be mentioned in the documentation.

Feel free to mail me.

Regards
    Martin.


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [1999-12-15 14:58 UTC] sas at cvs dot php dot net
Use strstr() if your needle might appear at position 0.
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Sat Oct 10 21:00:01 2026 UTC