isset vs empty vs is_null

PHP empty() vs is_null() vs isset():

   “”  “foo” NULL FALSE    0 undefined
empty() TRUE FALSE TRUE TRUE TRUE TRUE
is_null() FALSE FALSE TRUE FALSE FALSE TRUE  (ERROR)
isset() TRUE TRUE FALSE TRUE TRUE FALSE

If you want to check if there's any value other than null or undefined, use isset($var)
(because !is_null() generates a warning on undefined variables.)

If you want to check if the value is non-blank text or any number including zero, it gets trickier:

if (!empty($v) || (isset($v) && ($v === 0 || $v === '0'))) {
    // $v is non-blank text, true, 0 or '0'
    // $v is NOT an empty string, null, false or undefined
}

isset()

From PHP manual – isset():

isset — Determine if a variable is set and is not NULL

In other words, it returns true only when the variable is not null.

empty()

From PHP Manual – empty():

empty — Determine whether a variable is empty

In other words, it will return true if the variable is an empty string, false, array(), NULL, “0?, 0, and an unset variable.

is_null()

From PHP Manual – is_null():

is_null — Finds whether a variable is NULL

In other words, it returns true only when the variable is null. is_null() is opposite of isset(), except for one difference that isset() can be applied to unknown variables, but is_null() only to declared variables.


isset() will check if the variable is set, ie

<?php

echo isset($var); // false

$var = 'hello';

empty() will check if the variable is empty, ie

<?php

$emptyString = '';

echo empty($emptyString); // true

is_null() will check for NULL which is different from empty, because it's set to NULL not an empty string. (NULL might be a confusing concept)

Since your title is a string, I think you want to be using empty()

if (!isset($_REQUEST[$name_input_name]) || empty($_REQUEST[$name_input_name])) {
    $file->error = 'Please Enter a Title';
    return false;
}

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);
}

Tags:

Php