31 of 410 menu

The is_null Function

The is_null function checks if a variable contains the value null. Returns true if the variable's value is equal to null, and false otherwise.

Syntax

is_null(mixed $value): bool

Example

Let's check several values for null:

<?php $var1 = null; $var2 = 'abc'; $var3 = 0; var_dump(is_null($var1)); var_dump(is_null($var2)); var_dump(is_null($var3)); ?>

Code execution result:

true false false

Example

Let's check an uninitialized variable:

<?php var_dump(is_null($undefinedVar)); ?>

Code execution result:

true

Example

Comparison with other data types:

<?php $arr = [1, null, 3]; var_dump(is_null($arr[1])); ?>

Code execution result:

true

See Also

  • the is_array function,
    which checks if a variable is an array
  • the is_bool function,
    which checks if a variable is a boolean value
  • the isset function,
    which checks if a variable is set
byenru