Bug #70486 [NEW]: in_array/array_search returns false-positive

From: Date: Mon, 14 Sep 2015 06:38:30 +0000
Subject: Bug #70486 [NEW]: in_array/array_search returns false-positive
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-195990@lists.php.net to get a copy of this message
From: david at davidsteinsland dot net Operating system: PHP version: 5.6.13 Package: Arrays related Bug Type: Bug Bug description:in_array/array_search returns false-positive Description: ------------ The following test script is tested on PHP 5.4.43 (FreeBSD), PHP 5.5.20 (CentOS), PHP 5.6.10 (OSX). All tests say that "notHere" is in the array, and index 2. Before you say that the third parameter, "strict", will fix this. Consider: var_dump((int)'notHere' === 0); var_dump('notHere' === (string)0); I believe that it is the first casting that is being done internally in PHP? Would it not make more sense to cast it the other way around, as in no. 2? They look similar, but are completely different. In the first example I am searching for a string, "notHere". It's being casted to a integer (completely different value and type now), so it matches integer 0. In the second example I am still searching for a string, but now instead of casting the needle, the tested value is casted. The result of this comparison is false. Test script: --------------- <?php $data = ['str', 'str2', 0]; var_dump(in_array('notHere', $data)); var_dump(array_search('notHere', $data)); Expected result: ---------------- bool(false) bool(false) Actual result: -------------- bool(true) int(2) -- Edit bug report at https://bugs.php.net/bug.php?id=70486&edit=1 -- Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=70486&r=trysnapshot54 Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=70486&r=trysnapshot55 Try a snapshot (trunk): https://bugs.php.net/fix.php?id=70486&r=trysnapshottrunk Fixed in SVN: https://bugs.php.net/fix.php?id=70486&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=70486&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=70486&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=70486&r=needscript Try newer version: https://bugs.php.net/fix.php?id=70486&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=70486&r=support Expected behavior: https://bugs.php.net/fix.php?id=70486&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=70486&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=70486&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=70486&r=globals PHP 4 support discontinued: https://bugs.php.net/fix.php?id=70486&r=php4 Daylight Savings: https://bugs.php.net/fix.php?id=70486&r=dst IIS Stability: https://bugs.php.net/fix.php?id=70486&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=70486&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=70486&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=70486&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=70486&r=mysqlcfg

« previous php.bugs (#195990) next »