php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #45526 in_array returns wrong position if an "int 0" is in the array
Submitted: 2008-07-16 07:17 UTC Modified: 2008-07-16 09:59 UTC
From: clemens dot schwaighofer at tequila dot jp Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.2.6 OS: Linux
Private report: No CVE-ID: None
 [2008-07-16 07:17 UTC] clemens dot schwaighofer at tequila dot jp
Description:
------------
if the haystack array has an "int 0" in the array and the needle does not exists, true is returned. This is only the case in none strict setting, but it is still a bug.

Please do not close this as bogus and refer to the documentation and the strict flag. The flag works as can be seen with the "string 0" in the search part.

But it is wrong to return true if complete different character (char f) is searched and it returns true for "int 0"

Reproduce code:
---------------
$char = array('a', 'f', '0');
$array_search_data = array ('a', 'b', 'c', 0, '0');
print "Valid: ".print_r($array_search_data, 1)."<br>";
print "<br>";
foreach ($char as $_char)
{
        print "I[$_char] (false): in array: ".in_array($_char, $array_search_data)." | array search: ".array_search($_char, $array_search_data)."<br>";
        print "I[$_char] (true): in array: ".in_array($_char, $array_search_data, true)." | array search: ".array_search($_char, $array_search_data, true)."<br>";
        print "<br>";                                                                                                                                 }

Expected result:
----------------
in_array should return false for the search of 'f' and not true or the position of "int 0" in the array.

Actual result:
--------------
in_array (and also array_search) return true (or the position of "int 0" if a needle cannot be found in the array.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-07-16 09:50 UTC] jani@php.net
This is the actual result with proper code:
# php -r '$arr = array ("a", "b", "c", 0, "0"); var_dump(in_array("f", $arr, true));'
bool(false)

Without checkin the type you get "interesting" results. This is the same as:

# php -r 'var_dump("f" == 0);'
bool(true)

vs.

# php -r 'var_dump("f" === 0);'
bool(false)

RTFM: http://www.php.net/manual/en/types.comparisons.php
 [2008-07-16 09:59 UTC] clemens dot schwaighofer at tequila dot jp
still, without true "in_array" can return so bogus unusable results that the strict check should be on by default.

if I search a string in an array and it finds "int 0" and returns true, thats really bogus.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Jul 01 06:01:35 2025 UTC