note 86695 added to function.in-array
| From: | james dot ellis at gmail dot com | Date: | Thu, 30 Oct 2008 05:17:40 +0000 |
| Subject: | note 86695 added to function.in-array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-145962@lists.php.net to get a copy of this message | ||
Be aware of oddities when dealing with 0 (zero) values in an array...
This script:
<?php
$array = array('testing',0,'name');
var_dump($array);
//this will return true
var_dump(in_array('foo', $array));
//this will return false
var_dump(in_array('foo', $array, TRUE));
?>
It seems in non strict mode, the 0 value in the array is evaluating to boolean FALSE and in_array
returns TRUE. Use strict mode to work around this peculiarity.
This only seems to occur when there is an integer 0 in the array. A string '0' will return
FALSE for the first test above (at least in 5.2.6).
----
Server IP: 117.104.160.194
Probable Submitter: 58.6.148.157
----
Manual Page -- http://www.php.net/manual/en/function.in-array.php
Edit -- https://master.php.net/note/edit/86695
Del: integrated -- https://master.php.net/note/delete/86695/integrated
Del: useless -- https://master.php.net/note/delete/86695/useless
Del: bad code -- https://master.php.net/note/delete/86695/bad+code
Del: spam -- https://master.php.net/note/delete/86695/spam
Del: non-english -- https://master.php.net/note/delete/86695/non-english
Del: in docs -- https://master.php.net/note/delete/86695/in+docs
Del: other reasons-- https://master.php.net/note/delete/86695
Reject -- https://master.php.net/note/reject/86695
Search -- https://master.php.net/manage/user-notes.php