PHP has different functions like Empty() Isset() and is_null(). It is used to test the value of variables. Read more to understand different scenarios it is useful.
What is empty() in PHP?
The function empty() will use to determine whether the variable is empty or not. Normally in two scenarios it will be considered as an empty.
1) If the variable does not exist. The particular variable which we are using in the empty function which does not exist.
Output:
It will return true. Because above code does not have variable called $a.
2) If the variable is set as a false. That time also it will be considered as an empty.
Output:
It will return true because the variable $a is set as false.
Also, no warning will be generated even if the variable does not exist and this empty() function will only support variables.
What is isset() in PHP?
The isset() function is used to determine whether the variable is set or not. If the variable is set it will return true otherwise it will return false.
Output:
This is a fruit name.
After setting the variable if the user will unset() the variable means then isset() function will be false.
Output:
This is a fruit name
After unset
Variable is not set
What is is_null() in PHP?
This is_null() function is used to check whether the variable is null or not. If the variable is null it will return true otherwise false.
Companionship among empty(), isset() and is_null() in PHP
In PHP these three functions (empty(), isset(), is_null()) are mainly used to check the value of the variable. Many a times isset() and empty() will be used but both are opposite behavior. The is_null() function also one of the important function but it will only used for checking if the variable is null or not.