Bug #49657 [Nab]: array_search returns null instead of false
| From: | cmb@php.net | Date: | Tue, 11 Oct 2016 15:37:42 +0000 |
| Subject: | Bug #49657 [Nab]: array_search returns null instead of false | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-204604@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=49657&edit=1
ID: 49657
Updated by: cmb@php.net
Reported by: pvenakis at efrontlearning dot net
Summary: array_search returns null instead of false
Status: Not a bug
Type: Bug
-Package: Unknown/Other Function
+Package: Arrays related
Operating System: windows
PHP Version: 5.3.0
-Assigned To:
+Assigned To: cmb
Block user comment: N
Private report: N
New Comment:
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>.
Previous Comments:
------------------------------------------------------------------------
[2012-06-30 10:01:29] 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.
------------------------------------------------------------------------
[2011-11-26 16:11:57] 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.
------------------------------------------------------------------------
[2009-09-25 07:14:46] 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
------------------------------------------------------------------------
[2009-09-24 12:17:22] pajoye@php.net
That's expected. Returning NULL on invalid argument(s) is correct.
------------------------------------------------------------------------
[2009-09-24 12:03:47] 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
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=49657&edit=1