php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #54616 in_array return true if the haystack contain 0
Submitted: 2011-04-27 23:45 UTC Modified: 2011-04-28 00:18 UTC
From: kozo at alphanet dot ro Assigned:
Status: Not a bug Package: Unknown/Other Function
PHP Version: 5.2.17 OS: win, linux
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: kozo at alphanet dot ro
New email:
PHP Version: OS:

 

 [2011-04-27 23:45 UTC] kozo at alphanet dot ro
Description:
------------
The in_array function return true if the haystack have an element with value 0

Test script:
---------------
if (in_array('something', array(0)) ) {
	echo 'something is in array';
} else {
	echo 'something is NOT in array';
}


if (in_array('something', array(0, 'element1', 'element2')) ) {
	echo 'something is in array';
} else {
	echo 'something is NOT in array';
}


Expected result:
----------------
something is NOT in array

Actual result:
--------------
something is in array

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-04-28 00:18 UTC] rasmus@php.net
-Status: Open +Status: Bogus
 [2011-04-28 00:18 UTC] rasmus@php.net
That's what the 3rd param to the in_array() call is for. 
You are looking for a string in an array of integers. 
(int)'something' == 0 and 0 exists in the array. If you
want a strict check that does a type check as well, set
the strict flag. No bug here.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 17 14:04:04 2025 UTC