Will isset($foo)
always display the same result as !$foo
?
I have a peice of code where I'm getting php warnings for using:
if(!$foo){}
And I'm pretty sure that I should be using:
if(!isset($foo)){}
And that made me curious whether I'm changing the functionality here or not.
No.
Using a boolean negation operator !
a variable is casted to boolean. Boolean FALSE
is equal to NULL
(this is functionally however the same as isset()
), empty string, 0, empty array.
Using isset
no error is given if the variable does not exist. If you use !
with non-existent variable, E_NOTICE
is shown.
No.
One tests if a value is not set, the other tests if it is not true.
Compare:
<?php
$foo = 0;
if(!$foo){ echo 1; }
if(!isset($foo)){ echo 2; }
?>
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