php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #30000 in_array returns the wrong result with long strings
Submitted: 2004-09-06 17:02 UTC Modified: 2007-07-09 13:34 UTC
Votes:4
Avg. Score:4.5 ± 0.9
Reproduced:2 of 4 (50.0%)
Same Version:1 (50.0%)
Same OS:2 (100.0%)
From: rcnetto at yahoo dot com Assigned:
Status: Wont fix Package: Arrays related
PHP Version: 5.0.1 OS: Linux - Suse 9.1
Private report: No CVE-ID: None
 [2004-09-06 17:02 UTC] rcnetto at yahoo dot com
Description:
------------
I tried to check if a value existed in an array, but in_array did not return what I expected when I'm dealing with large strings (18 chars or longer).

I have PHP 5.0.1 compiled with Apache 2.0.50 and DBG v2.11.26 running;



Reproduce code:
---------------
<?php
    $needle = '123456789012345671';
    $arrayA = array('123456789012345672'); // check number is different
    var_dump(in_array($needle, $arrayA)); // the correct output is FALSE, but TRUE is printed
?>

Expected result:
----------------
bool(false)

OBS: $needle does not exists in $arrayA (last number is different), that's why it should return FALSE.

Actual result:
--------------
bool(true)

Notes: when the value of the variables are letters, in_array works fine with the same quantity of chars.
i.e:
<?php
    $needle = 'abcdefghijklmnopqr1';
    $arrayA = array('abcdefghijklmnopqr2'); // last char is different
    var_dump(in_array($needle, $arrayA));
?>

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-10-06 01:57 UTC] iliaa@php.net
Numeric strings get converted to integers when used as array keys. Because ints are limited to 32 bits (on x86) they end up being 0 on very large numbers.
 [2004-10-06 13:40 UTC] rcnetto at yahoo dot com
Interesting,... using the strict parameter will force in_array to check types (or not converting to int??). Then the result will be ok.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 13:01:31 2024 UTC