php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #26232 in_array produces true when bool true is in haystack
Submitted: 2003-11-12 22:11 UTC Modified: 2003-11-14 10:21 UTC
From: me at my dot house Assigned:
Status: Not a bug Package: Feature/Change Request
PHP Version: 4.3.2, 5b2 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: me at my dot house
New email:
PHP Version: OS:

 

 [2003-11-12 22:11 UTC] me at my dot house
Description:
------------
If the haystack contains the boolean true, in_array returns true!!
Check this (PHP 4.2.3-8 debian package) :



Reproduce code:
---------------
<?php
$r=array("fzsgsdgsd","reazrazr","rezarzearzae",true);
$ret=in_array("tsuser_id",$r);

print $ret;

}

?> 

Expected result:
----------------
false

Actual result:
--------------
true

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-11-13 03:27 UTC] helly@php.net
marcus@zaphod /usr/src/php5 $ php -r 'var_dump(in_array("x",array(1,2,3,false)));'
bool(false)
marcus@zaphod /usr/src/php5 $ php -r 'var_dump(in_array("x",array(1,2,3,true)));'
bool(true)
 [2003-11-13 10:55 UTC] jay@php.net
Isn't this sort of expected? "x" should be converted to a 
boolean and compared to true, and obviously true == true.  
 
J 
 [2003-11-13 19:34 UTC] iliaa@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

This is to be expected. Jay's explanation clearly identifies the fault in the PHP script. In this situation you should specify the 3rd parameter to in_array(), which would make comparison strict. 
 [2003-11-14 02:42 UTC] helly@php.net
Yes it is correct BUT in this case i suggest we add an option to the function to switch to === checks.
 [2003-11-14 10:21 UTC] jay@php.net
That's what Ilia was getting at. If the third parameter to 
in_array() is true, types are also checked a-la ===. 
 
As per your example... 
 
jay@monty jay $ php -r 
'var_dump(in_array("x",array(1,2,3,true)));' 
bool(true) 
jay@monty jay $ php -r 
'var_dump(in_array("x",array(1,2,3,true), true));' 
bool(false) 
 
J 
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 10 12:01:33 2025 UTC