php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #68830 in_array() always returns true when associative array contains 0 as the value
Submitted: 2015-01-14 00:57 UTC Modified: 2015-01-14 01:28 UTC
From: christopher dot r dot haley at gmail dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.5.20 OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: christopher dot r dot haley at gmail dot com
New email:
PHP Version: OS:

 

 [2015-01-14 00:57 UTC] 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.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-01-14 01:07 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2015-01-14 01:07 UTC] 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 01:14 UTC] 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:23 UTC] 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:28 UTC] 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.
 [2019-06-06 11:42 UTC] chrisevans07a at gmail dot com
Hello,

in_array(true, ['a', 'b', 'c']);

returns TRUE!

I'm using PHP 7.2
 [2019-06-06 12:13 UTC] christopher dot r dot haley at gmail dot com
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
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat Jun 21 07:01:34 2025 UTC