'Does accessing a false Boolean value in $_POST return false or null?
I am sending x = false; Boolean value through the POST method. Will Request::post['x'] or $_POST['x'] return Boolean false or null?
If I try (!isset($_POST['x'])) it gives me true, but I don't understand why.
Solution 1:[1]
Note that using ! (the NOT logical operator), means that (!isset($_POST['x'] )) will return true if x is not set (i.e. null).
All data in $_POST is untyped; it is all a string. If you need to send Boolean values, one option would be to compare a string to "true" or "false".
if ($_POST['x'] === "true") {
// True
} elseif ($_POST['x'] === "false") {
// False
} else {
// Error - not equal to true or false string
}
A note regarding comparison operators in PHP:
$x == $y returns true if $x is equal to $y
$x === $y returns true if $x is equal to $y and they are of the same type
Solution 2:[2]
If $_POST['x'] will contain anything, except null, then isset() will return true.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Chris |
| Solution 2 | zen |
