isset vs empty vs is_null

is_null() emits a WARNING if variable is not set, but isset() and empty() don’t.

$a - variable with not null value (e.g. TRUE)
$b - variable with null value. `$b = null;`
$c - not declared variable
$d - variable with value that cast to FALSE (e.g. empty string, FALSE or empty array)
$e - variable declared, but without any value assigned
$a->a - declared, but not assigned object property. (`public $a;`)
A::$a - declared, but not assigned static class property.

         |   $a  |   $b  |   $c  |   $d  |   $e  | $a->a | A::$a |
---------+-------+-------+-------+-------+-------+-------+-------+
is_null()| FALSE | TRUE  |TRUE*W | FALSE | TRUE*W| TRUE  | TRUE  |
---------+-------+-------+-------+-------+-------+-------+-------+
isset()  | TRUE  | FALSE | FALSE | TRUE  | FALSE | FALSE | FALSE |
---------+-------+-------+-------+-------+-------+-------+-------+
empty()  | FALSE | TRUE  | TRUE  | TRUE  | TRUE  | TRUE  | TRUE  |
---------+-------+-------+-------+-------+-------+-------+-------+
null === | FALSE | TRUE  |TRUE*W | FALSE | TRUE*W| TRUE  | TRUE  |
---------+-------+-------+-------+-------+-------+-------+-------+
null ==  | FALSE | TRUE  |TRUE*W | TRUE  | TRUE*W| TRUE  | TRUE  |
---------+-------+-------+-------+-------+-------+-------+-------+

TRUE*W - function return TRUE, but same time emits WARNING.

On empty() function documentation page you can read, that:

The following things are considered to be empty:

….

$var; (a variable declared, but without a value)

It can be misleading that code $var; is defining a variable, but does not assign any value to it, but it is wrong. Variable $var is still undefined and type recognize functions, like is_null() emits warnings if you pass $var as an argument.

But it is not right for unsettled class or object properties. Declaring them without assigning some value automatically assigns NULL.

UPD Typed properties in PHP 7.4 DO NOT assigned by NULL by default. If you does not set any value to them, they are considered as unassigned.

Some low level descriptions:

isset() and empty() are core functions, that will be compiled directly to specific opcode according to zval type:

ZEND_ISSET_ISEMPTY_THIS
ZEND_ISSET_ISEMPTY_CV
ZEND_ISSET_ISEMPTY_VAR
ZEND_ISSET_ISEMPTY_DIM_OBJ
ZEND_ISSET_ISEMPTY_PROP_OBJ
ZEND_ISSET_ISEMPTY_STATIC_PROP

Furthermore they will compile by the same function zend_compile_isset_or_empty

Function is_null() is type recognizer function, like is_numeric, is_recource, is_bool, etc. And will be called like user-defined function with opcodes INIT_FCALL_BY_NAME/DO_FCALL_BY_NAME and so.

/* {{{ proto bool is_null(mixed var)
   Returns true if variable is null
   Warning: This function is special-cased by zend_compile.c and so is usually bypassed */
PHP_FUNCTION(is_null)
{
    php_is_type(INTERNAL_FUNCTION_PARAM_PASSTHRU, IS_NULL);
}

Leave a Comment