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 -- ht
tp://www.php.net/manual/en/function.in-array.php
Edit -- https://master
.php.net/note/edit/78524
Del: integrated -- h
ttps://master.php.net/note/delete/78524/integrated
Del: useless -- http
s://master.php.net/note/delete/78524/useless
Del: bad code -- htt
ps://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 -- http
s://master.php.net/note/delete/78524/in+docs
Del: other reasons-- https://mast
er.php.net/note/delete/78524
Reject -- https://mast
er.php.net/note/reject/78524
Search -- https://
master.php.net/manage/user-notes.php
--
PHP Notes Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub
.php
|