note 78524 added to function.in-array
| From: | fd0tfesserattgmxd0tnet at osu1 dot php dot net | Date: | Tue, 16 Oct 2007 10:20:00 +0000 |
| Subject: | note 78524 added to function.in-array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-131420@lists.php.net to get a copy of this message | ||
In case you have to check for unknown or dynamic variables in an array, you can use the following
simple work-around to avoid misleading checks against empty and zero values (and only these
"values"!):
<?php
in_array($value, $my_array, empty($value) && $value !== '0');
?>
The function empty() is the right choice as it turns to true for all 0, null and ''.
The '0' value (where empty() returns true as well) has to be excluded manually (as this is
handled by in_array correctly!).
Examples:
<?php
$val = 0;
$res = in_array($val, array('2007'));
?>
leads incorrectly to true where
<?php
$val = 0;
$res = in_array($val, array('2007'), empty($val) && $val !== '0');
?>
leads correctly to false (strict check!) while
<?php
$val = 2007;
$res = in_array($val, array('2007'), empty($val) && $val !== '0');
?>
still correctly finds the '2007' ($res === true) because it ignores strict checking for
that value.
----
Server IP: 69.147.83.197
Probable Submitter: 84.189.100.161
----
Manual Page -- http://www.php.net/manual/en/function.in-array.php
Edit -- https://master.php.net/note/edit/78524
Del: integrated -- https://master.php.net/note/delete/78524/integrated
Del: useless -- https://master.php.net/note/delete/78524/useless
Del: bad code -- https://master.php.net/note/delete/78524/bad+code
Del: spam -- https://master.php.net/note/delete/78524/spam
Del: non-english -- https://master.php.net/note/delete/78524/non-english
Del: in docs -- https://master.php.net/note/delete/78524/in+docs
Del: other reasons-- https://master.php.net/note/delete/78524
Reject -- https://master.php.net/note/reject/78524
Search -- https://master.php.net/manage/user-notes.php