PHP Tutorials

What Is Null in PHP? Explained Simply for Beginners

PHP null means a variable has no value. Learn how to detect null and use the null coalescing operator safely.

Serhat ALPASLAN1 min read

PHP

What is null in PHP? In PHP, the null type is a special case. NULL has a single value, and it indicates that the variable has no value.

<?php 
$eposta= null; 
var_dump($email); // NULL
?>

The output of the example above will be NULL. Also, if we want to remove a value from a variable with unset(), we assign null to the variable. For example;

<?php 
$eposta= 'webmaster@phptutorial.net'; 
unset($eposta); 
var_dump($eposta); // NULL
?>

Although PHP behaves as partly case-sensitive, it is case-insensitive for some words. Therefore, NULL and null can be used without any difference.

How Do We Know Whether a Variable Is Null?

We use the is_null() function to determine whether a variable is null. If the function returns true, the variable has no value, meaning it is empty. If it returns false, the variable has a value.

<?php 
$email = null; 
var_dump(is_null($email)); // bool(true) 
$home = 'phptutorial.net'; 
var_dump(is_null($home)); // bool(false)
?>

From the official PHP website you can get detailed information. Also, don’t forget to check out the other PHP lessons;

Share it if it helped

Write a comment