Difference between isset() and empty() Functions in PHP

PHP provides this inbuilt function called empty() that can be used to determine if a variable is empty or not.

Example: This example shows how to use PHP’s empty() function.

<?php
$var_1 = 0;
$var_2 = 0.0;
$var_3 = “0”;
$var_4 = NULL;
$var_5 = false;
$var_6 = array();
$var_7 = “”;
// For value 0 as integer
empty($var_1) ? print_r(“True\n”) : print_r(“False\n”);
Output – True
// For value 0.0 as float
empty($var_2) ? print_r(“True\n”) : print_r(“False\n”);
// For value 0 as string
empty($var_3) ? print_r(“True\n”) : print_r(“False\n”);
// For value Null
empty($var_4) ? print_r(“True\n”) : print_r(“False\n”);
// For value false
empty($var_5) ? print_r(“True\n”) : print_r(“False\n”);
// For array
empty($var_6) ? print_r(“True\n”) : print_r(“False\n”);
// For empty string
empty($var_7) ? print_r(“True\n”) : print_r(“False\n”);
// For not declare $var8
empty($var_8) ? print_r(“True\n”) : print_r(“False\n”);
?>
Output –
True
True
True
True
True
True
True
True

isset() Function: If a variable is declared and its value does not equal NULL, it can be determined using PHP’s built-in isset function.

Parameters: As previously mentioned above and detailed below, this function takes one or more parameters.

$var: The variable that has to be checked is contained there.
The list of additional variables is contained in $..
Return Value: If the variable exists and its value does not equal NULL, it returns TRUE; if not, it returns FALSE.

Example 2: Below examples determines the isset() function in PHP:

<?php
¬†$str = “Anyhow”;
// Check value of variable is set or not
if(isset($str)) {
echo “Value of variable is set”;
}
else {
echo “Value of variable is not set”;
}
$arr = array();
// Check value of variable is set or not
if( !isset($arr[0]) ) {
echo “\nArray is Empty”;
}
else {
echo “\nArray is not empty”;
}
?>
Output –
Value of variable is set

Array is Empty