note 117419 added to function.in-array
| From: | KrisSpencer | Date: | Mon, 08 Jun 2015 17:35:18 +0000 |
| Subject: | note 117419 added to function.in-array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-202769@lists.php.net to get a copy of this message | ||
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".
----
Server IP: 72.52.91.14
Probable Submitter: 98.251.242.46
----
Manual Page -- http://php.net/manual/en/function.in-array.php
Edit -- https://master.php.net/note/edit/117419
Del: integrated -- https://master.php.net/note/delete/117419/integrated
Del: useless -- https://master.php.net/note/delete/117419/useless
Del: bad code -- https://master.php.net/note/delete/117419/bad+code
Del: spam -- https://master.php.net/note/delete/117419/spam
Del: non-english -- https://master.php.net/note/delete/117419/non-english
Del: in docs -- https://master.php.net/note/delete/117419/in+docs
Del: other reasons-- https://master.php.net/note/delete/117419
Reject -- https://master.php.net/note/reject/117419
Search -- https://master.php.net/manage/user-notes.php