note 117419 deleted from function.in-array by cmb
| From: | cmb@php.net | Date: | Mon, 21 Jan 2019 14:21:26 +0000 |
| Subject: | note 117419 deleted from function.in-array by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-210088@lists.php.net to get a copy of this message | ||
Note Submitter: Kris Spencer
----
if using in_array to evaluate a boolean in a _GET or _POST, be sure to put single quotes around your
"haystack values" or you will get a false result.
example of false/incorrect result:
[whatever.php?var=1 -- $_GET['var'] is 1]
if(!in_array($_GET['var'], array(0, 1), true)){
echo 'not in array';
}
else{
echo 'is in array';
}
output is "not in array" even though 1 === 1. Same is true without strict evaluation.
example of correct evaluation/result:
[whatever.php?var=1 -- $_GET['var'] is 1]
if(!in_array($_GET['var'], array('0', '1'), true)){
echo 'not in array';
}
else{
echo 'is in array';
}
output is now correct; "is in array".