PHP check if False or Null

Checking variable ( a few examples )

if(is_null($x) === true) // null
if($x === null) // null
if($x === false)
if(isset($x) === false) // variable undefined or null
if(empty($x) === true) // check if variable is empty (length of 0)

Isset() checks if a variable has a value including ( False , 0 , or Empty string) , But not NULL. Returns TRUE if var exists; FALSE otherwise.

On the other hand the empty() function checks if the variable has an empty value empty string , 0, NULL ,or False. Returns FALSE if var has a non-empty and non-zero value.


For returns from functions, you use neither isset nor empty, since those only work on variables and are simply there to test for possibly non-existing variables without triggering errors.

For function returns checking for the existence of variables is pointless, so just do:

if (!my_function()) {
    // function returned a falsey value
}

To read about this in more detail, see The Definitive Guide To PHP's isset And empty.

Tags:

Php

Isset