8. NULL data type
Null is a variable with no value. It is called a special data type in PHP.
A variable is null if,
1. It has been assigned the constant NULL
2. It has not been set to any value.
3. It has been unset()
|
Value NULL is case insensitive.
|
1. <?php
2. $var = NuLl;
3. echo var_dump($var);
4.
5. $var1;
6. echo var_dump($var1); //Will throw an Error NULL
7.
8. echo "<br>";
9. $var2 ="Mike";
10. echo var_dump($var2);
11.
12. unset($var2);
13. echo var_dump($var2); //Will throw an Error NULL
14. ?>
Output
NULL
Notice: Undefined variable: var1 in E:\xampp\htdocs\xampp\Tutorials\test.php on line 6
NULL
string(4) "Mike"
Notice: Undefined variable: var2 in E:\xampp\htdocs\xampp\Tutorials\test.php on line 13
NULL
As we can see $var is defined as NUll (as Null is case insensitive).
$var1 is undefined (variable is declare without assigning value to it is null)
$var2 is defined as Mike and later being uset using
unset() function, so it result to null.(this is called as
Casting variable to null)