Curriculum
This tutorial will teach you about the PHP NULL type and how to determine whether or not a variable is null.
In PHP, the null value is a special type. The null type has a single value that is also null. In fact, null indicates that a variable has no value.
When you assign null to a variable, it becomes null:
<?php $email = null; var_dump($email); // NULL
Furthermore, when you use the unset() function to remove a variable, the variable becomes null. As an example:
<?php $email = '[email protected]'; unset($email); var_dump($email); // NULL
PHP keywords are not case sensitive. As a result, NULL is case-insensitive. It means that you can represent the null value with null, Null, or NULL. As an example:
<?php $email = null; $first_name = Null; $last_name = NULL;
Maintaining consistency in your code is a good practise. If you use null in lowercase in one place, you should use it throughout your codebase.
The is_null() function is used to determine whether or not a variable is null. If a variable is null, the is null() function returns true; otherwise, it returns false. As an example:
<?php $email = null; var_dump(is_null($email)); // bool(true) $home = 'phptutorial.net'; var_dump(is_null($home)); // bool(false)
You can also use the identical operator === to determine whether a variable is null or not. As an example:
<?php $email = null; $result = ($email === null); var_dump($result); // bool(true) $home= 'phptutorial.net'; $result = ($home === null); var_dump($result); // bool(false)