php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #49657 array_search returns null instead of false
Submitted: 2009-09-24 12:03 UTC Modified: 2016-10-11 15:37 UTC
From: pvenakis at efrontlearning dot net Assigned: cmb (profile)
Status: Not a bug Package: Arrays related
PHP Version: 5.3.0 OS: windows
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem:
11 + 43 = ?
Subscribe to this entry?

 
 [2009-09-24 12:03 UTC] pvenakis at efrontlearning dot net
Description:
------------
array_search returns false in 5.2.x, null in 5.3.0, when $haystack is not an array


Reproduce code:
---------------
$haystack = false; //Anything not an array
$result = array_search(1, $haystack);
var_dump($result);


Expected result:
----------------
bool(false)

Actual result:
--------------
null

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-09-24 12:17 UTC] pajoye@php.net
That's expected. Returning NULL on invalid argument(s) is correct.
 [2009-09-25 07:14 UTC] pvenakis at efrontlearning dot net
Thanks for the quick response. It's fine that it returns null, but since this changed from 5.2 to 5.3 it would be nice if it were mentioned in the changelog or in the documents. 
Best Regards, Periklis
 [2011-11-26 16:11 UTC] rodrigovb at gmail dot com
I'd like to advise you about the impact of this change in the company I work. Based on the last return value (FALSE) we had 40 php files with some lines checking the result of array_search (=== false). This is a small company with 30 employees (and 40 php files to be changed). 

The concern of the owner is if PHP is sustainable enough to keep investing in develop instead of maintenance due to this changes.

Please write the change log (or point the link with the log) cointaining the  real needs of this ghange.
 
In my point of view the impact of return value changes of any function could be severe, specially in bigger companies.
 [2012-06-30 10:01 UTC] phillip dot berndt at googlemail dot com
I disagree. This function returning NULL is nothing I'd expect. Especically since this behavior still (it's 2012!) is not documented. This IS a bug. If not because of the "feature", at least one regarding missing documentation.

In PHP 4.2, you changed this function such that it returns FALSE in case of an error, which, as was pointed out above by the other commenter, is good, because it makes existence checking easy. Since FALSE !== NULL, things get pretty complicated now.
 [2016-10-11 15:37 UTC] cmb@php.net
-Package: Unknown/Other Function +Package: Arrays related -Assigned To: +Assigned To: cmb
 [2016-10-11 15:37 UTC] cmb@php.net
If unexpected input is passed to a function, the behavior and
return value are *undefined*. This is documented since 2006, see
<http://svn.php.net/viewvc?view=revision&revision=219184>.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 23 21:01:31 2024 UTC