In PHP, isset vs empty vs is_null functions are most important part to start learning PHP programming.
isset(), empty() and is_null() functions are PHP in-built functions which are mainly used to check the value of the variable or initialization of the variable.
- isset() is used to check the variable has a value or not and also the variable is not NULL.
- empty() checks the given variable is empty. And it returns a boolean value.
- is_null() is used to check the variable has a NULL value.
Here we started, isset vs empty vs is_null in PHP With Example.
PHP isset() Function
isset() is a inbuilt PHP function which is used to check the value of the variable is set or not.
The function returns the result as Boolean value True or False.
Syntax:
1 |
isset(variable); |
Example of PHP isset() Function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?php // PHP code to check without set variable value $variable; if( isset( $variable ) ) { echo "The variable value is set, checked by isset()"; }else{ echo "The variable value is not set, checked by isset()"; } echo "<br>"; // PHP code to check with set variable value $variable2 = 'hello world'; if( isset( $variable2 ) ) { echo "The variable value is set, checked by isset()"; }else{ echo "The variable value is not set, checked by isset()"; } ?> |
Output:
The variable value is not set, checked by isset()
The variable value is set, checked by isset()
PHP empty() Function
empty() is also an PHP inbuilt function which is used to check the variable’s value is empty or not.
If the value is empty then empty() function returns TRUE and if value is not empty then it returns FALSE.
Syntax:
1 |
empty( $variable ) |
Example of PHP empty() Function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?php // PHP Code to check first variable $variable; if( empty( $variable ) ) { echo "The variable value is empty, checked by empty()"; }else{ echo "The variable value is not empty, checked by empty()"; } echo "<br>"; // PHP Code to test variable with empty() $variable2 = 'PHP world!'; if( empty( $variable2 ) ) { echo "The variable value is empty"; }else{ echo "The variable value is not empty"; } ?> |
Output:
The variable value is empty, checked by empty()
The variable value is not empty
PHP is_null() Function
PHP is_null() Function is used to find the variable value is NULL or not.
This function returns the result as a Boolean form (TRUE / FALSE).
Syntax:
1 |
is_null( $variable ) |
Example of PHP is_null() Function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?php // PHP Code or scritp to explain php is_null() function $variable = null; if( is_null( $variable ) ) { echo "The variable value is null"; }else{ echo "The variable value is not null"; } echo "<br>"; // PHP Code to test variable with is_null() $variable2 = '20'; if( is_null( $variable2 ) ) { echo "The variable value is null"; }else{ echo "The variable value is not null"; } ?> |
Output:
The variable value is null
The variable value is not null
Complete Difference Between isset, empty, and is_null in PHP by Using Table
Functions | “” | “phpcoder” | NULL | FALSE | 0 | undefined |
empty() | TRUE | FALSE | TRUE | TRUE | TRUE | TRUE |
is_null() | FALSE | FALSE | TRUE | FALSE | FALSE | ERROR |
isset() | TRUE | TRUE | FALSE | TRUE | TRUE | FALSE |
Tested the above values in following PHP versions:
- PHP 7.4
- PHP 7.3
- PHP 7.2
- PHP 7.0
- PHP 5.6
- PHP 5.5
Conclusion
Here we discuss complete explanations of isset, empty and is_null function in PHP and what are the differences between these functions also.
To know more about these functions you can also check official site, http://www.php.net/manual/en/function.is-null.php
http://www.php.net/manual/en/function.empty.php
http://www.php.net/manual/en/function.isset.php
Also check:
- Get Current City State and Zip Code Using PHP
- Count Number of Visits Using PHP Cookies
- Send Mail Attachments with PHP Mail()
- Increase PHPMyAdmin Import Size Ubuntu and XAMPP
Happy Coding..!
[…] isset vs empty vs is_null in PHP With Example […]
[…] isset vs empty vs is_null in PHP With Example […]
[…] isset vs empty vs is_null in PHP With Example […]