Bug #68830 [Nab]: in_array() always returns true when associative array contains 0 as the value
| From: | christopher dot r dot haley at gmail dot com | Date: | Wed, 14 Jan 2015 01:28:42 +0000 |
| Subject: | Bug #68830 [Nab]: in_array() always returns true when associative array contains 0 as the value | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-189946@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=68830&edit=1
ID: 68830
User updated by: christopher dot r dot haley at gmail dot com
Reported by: christopher dot r dot haley at gmail dot com
Summary: in_array() always returns true when associative
array contains 0 as the value
Status: Not a bug
Type: Bug
Package: Scripting Engine problem
PHP Version: 5.5.20
Block user comment: N
Private report: N
New Comment:
Interesting, thank you! I wonder what js does that gives exactly the opposite result. At any rate, I
was wrong, this quite obviously not a bug in PHP.
Previous Comments:
------------------------------------------------------------------------
[2015-01-14 01:23:47] requinix@php.net
> Wow, I never knew [a string] == 0.
Non-numeric strings. To compare, PHP converts the string to a number (as opposed to converting the
number to a string), and since "b" is non-numeric it converts to 0.
$arr = array("a" => 123);
echo intval(in_array("123", $arr)); // 1
echo intval(in_array("123", $arr, true)); // 0
------------------------------------------------------------------------
[2015-01-14 01:14:46] christopher dot r dot haley at gmail dot com
Wow, I never knew [a string] == 0. That's a strange mechanic in PHP; different than JavaScript!
Thanks for the super fast response. You are right, you can close/delete this or whatever you need to
do.
------------------------------------------------------------------------
[2015-01-14 01:07:00] requinix@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to
report
a bug at http://bugs.php.net/how-to-report.php
in_array() does a loose comparison by default and 0 == "b". Use its third argument.
------------------------------------------------------------------------
[2015-01-14 00:57:16] christopher dot r dot haley at gmail dot com
Description:
------------
---
From manual page: http://www.php.net/function.in-array
---
From phpinfo(), the version is: 5.5.9-1ubuntu4.5 but that was not a choice in the drop down. It
seems to span over other versions as well.
Test script:
---------------
$arr = array("a" => 0);
echo intval(in_array("b", $arr));
Expected result:
----------------
Expected output is 0 (false) when the first argument is not present in second argument.
Actual result:
--------------
Output is 1 (true) regardless of the first argument.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=68830&edit=1