Bug #68830 [Com]: in_array() always returns true when associative array contains 0 as the value

From: Date: Thu, 06 Jun 2019 12:13:32 +0000
Subject: Bug #68830 [Com]: 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-221169@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 Comment 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: chrisevans07a, as the responder said 4 years ago, it's a loose comparison. To compare a string to a number, PHP converts the string to a number. To compare a string to a boolean, PHP converts a string to a boolean. See below: var_dump("a" == 0); // true var_dump("1" == 0); // false var_dump("a" == false); // false var_dump("0" == false); // true Previous Comments: ------------------------------------------------------------------------ [2019-06-06 11:42:52] chrisevans07a at gmail dot com Hello, in_array(true, ['a', 'b', 'c']); returns TRUE! I'm using PHP 7.2 ------------------------------------------------------------------------ [2015-01-14 01:28:41] christopher dot r dot haley at gmail dot com 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. ------------------------------------------------------------------------ [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. ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at https://bugs.php.net/bug.php?id=68830 -- Edit this bug report at https://bugs.php.net/bug.php?id=68830&edit=1

« previous php.bugs (#221169) next »