In various PHP tutorials I see this syntax -
if ($_POST) {
do something
}
I want to know whether this is equivalent to either isset or !(empty) (either one) or has different properties.
It attempts to evaluate the expression and cast it to boolean.
See 'Converting to boolean' at http://php.net/manual/en/language.types.boolean.php to see which values will be equivalent to true
and which to false
.
It does NOT however check for array key existence (i.e. isset
), so if you try if ($_GET['somekey'])
but somekey
does not exist, you will see PHP Notice: Undefined index: somekey
and then false
will be assumed.
The best practice would be to perform empty()
or isset()
checks manually first as fits.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With