php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #30158 strpos doesn't work as it should
Submitted: 2004-09-19 22:09 UTC Modified: 2004-09-20 15:34 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: a dot bendilas at zefxis dot gr Assigned:
Status: Not a bug Package: Strings related
PHP Version: 4.3.8 OS: WinXP SP2
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 this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: a dot bendilas at zefxis dot gr
New email:
PHP Version: OS:

 

 [2004-09-19 22:09 UTC] a dot bendilas at zefxis dot gr
Description:
------------
Although strpos returns the position of the needle correctly, when testing the return value with a boolean operator to see if it's true, it produces the opposite result.

Reproduce code:
---------------
$string = 'Once upon a time in America';
$srchstring = 'time';
$position = strpos($string, $srchstring);

echo 'String: '.$string.'<br />';
echo 'Search string: '.$srchstring.'<br />';
echo 'Position of search string: '.$position.'<br />';

if ((strpos($string, $srchstring) === true)) echo 'Search string is included in the original string!'; else echo 'Search string is not included in the original string!';


Expected result:
----------------
Search string is included in the original string!

Actual result:
--------------
Search string is not included in the original string!

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-09-20 00:18 UTC] rasmus@php.net
Why are you doing a === to true?  strpos is clearly documented to return a integer specifying the position in the string on a match or false on a failure.  It will never return true.
 [2004-09-20 08:53 UTC] a dot bendilas at zefxis dot gr
A note from the PHP manual on strstr:

If you only want to determine if a particular needle occurs within haystack, use the faster and less memory intensive function strpos() instead. 

A warning from the PHP manual on strpos:

This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE, such as 0 or "". Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.

As for why I'm doing a check for true, it's because '0' evaluates to false.

If my original string was 'time is money' then $position would be 0:

Reproduce code:
---------------
$string = 'time is money';
$srchstring = 'time';
$position = strpos($string, $srchstring);
echo 'String: '.$string.'<br />';
echo 'Search string: '.$srchstring.'<br />';
echo 'Position of search string: '.$position.'<br />';

if ((strpos($string, $srchstring) === true)) echo 'Search string is included in the original string!'; else echo 'Search string is not included in the original string!';


Expected result:
----------------
Search string is included in the original string!


Actual result:
--------------
String: time is money
Search string: time
Position of search string: 0
Search string is not included in the original string!
 [2004-09-20 09:12 UTC] a dot bendilas at zefxis dot gr
Apparently the PHP manual isn't so clear on how to use the === operator.

So, what it really means is that you should check for something like this:

$string = 'time is money';
$srchstring = 'time';
$position = strpos($string, $srchstring);

if (($position > 0) || ($position === 0)) echo 'Search string is included in the original string!'; else echo 'Search string is not included in the original string!';
 [2004-09-20 15:34 UTC] rasmus@php.net
No, it means you should do a !== false check.  Anything except false means it found it.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Apr 20 02:01:29 2024 UTC