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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
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

Add a Patch

Pull Requests

Add a Pull Request

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-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 09:01:28 2024 UTC