note 106319 added to function.in-array
| From: | beingmrkenny at gmail dot com | Date: | Thu, 27 Oct 2011 10:35:27 +0000 |
| Subject: | note 106319 added to function.in-array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-184163@lists.php.net to get a copy of this message | ||
Loose checking returns some crazy, counter-intuitive results when used with certain arrays. It is
completely correct behaviour, due to PHP's leniency on variable types, but in
"real-life" is almost useless.
The solution is to use the strict checking option.
<?php
// Example array
$array = array(
'egg' => true,
'cheese' => false,
'hair' => 765,
'goblins' => null,
'ogres' => 'no ogres allowed in this array'
);
// Loose checking -- return values are in comments
// First three make sense, last four do not
in_array(null, $array); // true
in_array(false, $array); // true
in_array(765, $array); // true
in_array(763, $array); // true
in_array('egg', $array); // true
in_array('hhh', $array); // true
in_array(array(), $array); // true
// Strict checking
in_array(null, $array, true); // true
in_array(false, $array, true); // true
in_array(765, $array, true); // true
in_array(763, $array, true); // false
in_array('egg', $array, true); // false
in_array('hhh', $array, true); // false
in_array(array(), $array, true); // false
?>
----
Server IP: 87.124.126.135
Probable Submitter: 88.96.237.202 (proxied: 88.96.237.202)
----
Manual Page -- http://www.php.net/manual/en/function.in-array.php
Edit -- https://master.php.net/note/edit/106319
Del: integrated -- https://master.php.net/note/delete/106319/integrated
Del: useless -- https://master.php.net/note/delete/106319/useless
Del: bad code -- https://master.php.net/note/delete/106319/bad+code
Del: spam -- https://master.php.net/note/delete/106319/spam
Del: non-english -- https://master.php.net/note/delete/106319/non-english
Del: in docs -- https://master.php.net/note/delete/106319/in+docs
Del: other reasons-- https://master.php.net/note/delete/106319
Reject -- https://master.php.net/note/reject/106319
Search -- https://master.php.net/manage/user-notes.php