Tuesday, November 19, 2024
Google search engine
HomeLanguagesDifference between isset() and empty() Functions

Difference between isset() and empty() Functions

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
 
    // PHP code to demonstrate the working
    // of empty() function
    $var1 = 0;
    $var2 = 0.0;
    $var3 = "0";
    $var4 = NULL;
    $var5 = false;
    $var6 = array();
    $var7 = "";
 
    // For value 0 as integer
    empty($var1) ? print_r("True\n") : print_r("False\n");
 
    // For value 0.0 as float
    empty($var2) ? print_r("True\n") : print_r("False\n");
 
    // For value 0 as string
    empty($var3) ? print_r("True\n") : print_r("False\n");
 
    // For value Null
    empty($var4) ? print_r("True\n") : print_r("False\n");
 
    // For value false
    empty($var5) ? print_r("True\n") : print_r("False\n");
 
    // For array
    empty($var6) ? print_r("True\n") : print_r("False\n");
 
    // For empty string
    empty($var7) ? print_r("True\n") : print_r("False\n");
 
    // For not declare $var8
    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 = "neveropen";
 
// 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

 

PHP program using both isset() and empty() functions:

 

PHP




<?php
 
// PHP function to demonstrate
// isset() and !empty() function
 
// initialize a variable
$num = '0';
 
// Check isset() function
if( isset ( $num ) ) {
    print_r( $num . " is set with isset function");
}
 
// Display new line
echo "\n";
 
// Initialize a variable
$num = 1;
 
// Check the !empty() function
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.

RELATED ARTICLES

Most Popular

Recent Comments