php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #63348 Unexpected behaviour with strpos()
Submitted: 2012-10-24 19:09 UTC Modified: 2012-10-24 23:26 UTC
From: aleksey dot v dot korzun at gmail dot com Assigned:
Status: Not a bug Package: *Programming Data Structures
PHP Version: 5.3.18 OS: *Nix
Private report: No CVE-ID: None
 [2012-10-24 19:09 UTC] aleksey dot v dot korzun at gmail dot com
Description:
------------
Please review code snippet below, expected behaviour is that code within if 
statement will not be executed (since the value was not found).

Test script:
---------------
<?php
$foobar = array();

if(strpos($foobar, 'something') !== false) {
	echo 'fail';
}
?>

Expected result:
----------------
It should not output 'fail'

Actual result:
--------------
Outputs fail

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-10-24 23:15 UTC] sixd@php.net
The code actually gives this message first:
PHP Warning:  strpos() expects parameter 1 to be string, array given

It's standard for PHP to behave like this for parameter parsing errors. E.g. all 
but a few of the str* functions are implemented using 'return' in this case. Only 
strrpos, strripos, strpbrk, (and substr_compare) use RETURN_FALSE.
 [2012-10-24 23:15 UTC] sixd@php.net
-Status: Open +Status: Not a bug
 [2012-10-24 23:26 UTC] aleksey dot v dot korzun at gmail dot com
I'm aware of the notice, but in my opinion that this should be handled within the 
function and return false .vs null as in no match was found.

- $0.2
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Wed Oct 20 13:03:32 2021 UTC