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

phpis_null(mixed $value): bool

Example

Let's check several values for null:

php<?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:

phptrue
false
false

Example

Let's check an uninitialized variable:

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

Code execution result:

phptrue

Example

Comparison with other data types:

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

Code execution result:

phptrue

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