Is isset ($ foo) functionally identical? $ Foo?

Will isset($foo) display the same result as !$foo ?

I have some code where I get php warnings to use:

 if(!$foo){} 

And I'm sure I should use:

 if(!isset($foo)){} 

And that made me curious if the functionality is changing here or not.

+4
source share
3 answers

No.

Using the logical negation operator ! , the variable is cast to boolean. Boolean FALSE is NULL (this is functional, but the same as isset() ), empty string, 0, empty array.

Using isset does not indicate an error if the variable does not exist. If you use ! with a nonexistent variable, E_NOTICE displayed.

+9
source

No.

One test if no value is given, other tests if it is not.

For comparison:

 <?php $foo = 0; if(!$foo){ echo 1; } if(!isset($foo)){ echo 2; } ?> 
+8
source

it depends on what foo is.

Foo can be set to 1 or some value that you are not applying to. Therefore, even if the variable is set, you can get unintended behavior

+1
source

Source: https://habr.com/ru/post/1486682/


All Articles