Open In App

Difference between isset() and empty() Functions

Last Updated : 24 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The isset() and empty() functions in PHP are used for variable checking but serve different purposes. isset() checks if a variable is set and not null, while empty() checks if a variable is considered empty, like 0, false, null, or an empty string.

empty() Functions

The empty() function in PHP checks whether a variable is considered “empty.” It returns true for values like 0, “0”, false, NULL, empty arrays, empty strings, or if the variable is not set.

Syntax:

bool empty ( $var )

Parameter: This function accepts a single parameter as shown in above syntax and described below.

  • $var: Variable to check whether it is empty or not.

Example: In this example we demonstrates the empty() function, which returns True for variables considered “empty” (e.g., 0, 0.0, “0”, NULL, false, empty arrays, and undefined variables).

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 in PHP checks whether a variable is set and is not NULL. It returns true if the variable has a value other than NULL, and false if it’s unset or NULL.

Syntax:

bool isset( mixed $var [, mixed $... ] )

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 : In this example we use isset() to check if a variable or array element is set. It confirms if $str is set and checks if the first element of $arr is defined.

PHP
<?php

$str = "GeeksforGeeks";

// 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:

Example: In this example we demonstrates isset() and !empty() functions. It first checks if $num is set with isset(), then verifies if $num is not empty using the !empty() function.

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:

Aspectsisset() Functionempty() Function
PurposeChecks if a variable is declared and is not NULL.Checks if a variable is empty or contains a falsy value.
Undefined VariablesGenerates a warning or notice if the variable does not exist.Does not generate any warning for undefined variables.
Return ValueReturns true if the variable is set and not NULL.Returns true if the variable is empty (0null, etc.).
Handling of Falsy ValuesReturns true for 0"0"false, and empty strings.Returns true for 0"0"falsenull, and empty strings.
Use CaseUsed to check if a variable exists or is defined.Used to check if a variable has no value or is empty.


Next Article

Similar Reads