The empty() function is an inbuilt function in PHP that is used to check whether a variable is empty or not.
These values are considered to be empty values:
- “” ( an empty string)
- 0 ( 0 as an integer)
- 0.0 ( 0 as a float)
- “0” ( 0 as a string)
- NULL
- FALSE
- array() (an empty array)
Example: Below example illustrate the empty() function in PHP.
PHP
<?php
$var1 = 0;
$var2 = 0.0;
$var3 = "0" ;
$var4 = NULL;
$var5 = false;
$var6 = array ();
$var7 = "" ;
empty ( $var1 ) ? print_r( "True\n" ) : print_r( "False\n" );
empty ( $var2 ) ? print_r( "True\n" ) : print_r( "False\n" );
empty ( $var3 ) ? print_r( "True\n" ) : print_r( "False\n" );
empty ( $var4 ) ? print_r( "True\n" ) : print_r( "False\n" );
empty ( $var5 ) ? print_r( "True\n" ) : print_r( "False\n" );
empty ( $var6 ) ? print_r( "True\n" ) : print_r( "False\n" );
empty ( $var7 ) ? print_r( "True\n" ) : print_r( "False\n" );
empty ( $var8 ) ? print_r( "True\n" ) : print_r( "False\n" );
?>
|
Output
True
True
True
True
True
True
True
True
isset() Function: The isset() function is an inbuilt function in PHP that is used to determine if the variable is declared and its value is not equal to NULL.
Parameters: This function accepts one or more parameters as mentioned above and described below.
- $var: It contains the variable which needs to check.
- $…: It contains the list of other variables.
Return Value: It returns TRUE if var exists and its value is not equal to NULL and FALSE otherwise.
Example 2: Below examples illustrate the isset() function in PHP:
PHP
<?php
$str = "GeeksforGeeks" ;
if (isset( $str )) {
echo "Value of variable is set" ;
}
else {
echo "Value of variable is not set" ;
}
$arr = array ();
if ( !isset( $arr [0]) ) {
echo "\nArray is Empty" ;
}
else {
echo "\nArray is not empty" ;
}
?>
|
Output
Value of variable is set
Array is Empty
PHP program using both isset() and empty() functions:
PHP
<?php
$num = '0' ;
if ( isset ( $num ) ) {
print_r( $num . " is set with isset function" );
}
echo "\n" ;
$num = 1;
if ( ! empty ( $num ) ) {
print_r( $num . " is set with !empty function" );
}
?>
|
Output:
0 is set with isset function
1 is set with !empty function
Difference between isset() and empty() function:
isset() Function
|
empty() Function
|
The isset() function is an inbuilt function in PHP that is used to determine if the variable is declared and its value is not equal to NULL. |
The empty() function is an inbuilt function in PHP that is used to check whether a variable is empty or not. |
The isset() function will generate a warning or e-notice when the variable does not exists. |
The empty() function will not generate any warning or e-notice when the variable does not exists. |
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
23 Sep, 2021
Like Article
Save Article