PHP isset() Function

What is PHP isset() Function?

PHP isset() function is used to check whether a variable is declared with value in the script. Here, value means any value other than NULL. So, according to the isset() function, a variable is considered as set if it is declared and has a non-null value.

Syntax:

isset(variable1, variable2, variable3…)

Parameters:

variable1 (required): Variable to be checked.

variable2, variable3… (optional): More variables to be checked.

Return Values:

For a single parameter-

  • The function returns TRUE only if the variable is declared with any value other than NULL. Check example 1.
  • The function returns FALSE for any other cases. For example- (Check example 2)
    • A variable with NULL constant.
    • A variable without any value.
    • An undeclared variable.
    • A variable unset by unset() function.
Variable Declared?Variable Valueisset() Return
YesNOT NULLTRUE
YesNULLFALSE
Yes\0FALSE

For multiple parameters- (Check example 3)

  • The function returns TRUE if all the variables are declared with values.
  • The function will return FALSE if at least one variable doesn’t exist.
  • The function will return FALSE if at least one variable declared with NULL values.

Examples:

Example 1:

<?php
$language = "PHP";
var_dump(isset($language));

echo "<br />";
$language = "";
var_dump(isset($language));
?>

Output:

bool(true)
bool(true)

Explanation:

The variable $language has the value, so, iseet() function returns TRUE (line 3). The variable $language has a value of empty string (line 6) but it is not NULL, so, iseet() function returns TRUE (line 7). As there is no variable named $newVariable, so, iseet() function returns FALSE (line 7).

Example 2:

<?php
$language = NULL;
var_dump(isset($language));

echo "<br />";
$language;
var_dump(isset($language));

echo "<br />";
var_dump(isset($newVariable));

echo "<br />";
$language = "PHP";
unset($language);
var_dump(isset($language));
?>

Output:

bool(false)
bool(false)
bool(false)
bool(false)

Explanation:

As the variable $language has the value NULL, so, iseet() function returns FALSE (line 3). As the variable $language has declared with no value, so, iseet() function returns FALSE (line 7). As there is no variable named $newVariable, so, iseet() function returns FALSE (line 10). Though the variable $language has declared with a value but, it is destroyed with unset() function. So, the variable doesn’t exist anymore. Hence, iseet() function returns FALSE (line 15).

Example 3:

<?php
$language1 = "PHP";
$language2 = "JAVA";
var_dump(isset($language1, $language2));

echo "<br />";
$language1 = "PHP";
$language2 = "JAVA";
var_dump(isset($language1, $language2, $language3));

echo "<br />";
$language1 = "PHP";
$language2 = "JAVA";
$language2 = NULL;
var_dump(isset($language1, $language2, $language3));
?>

Output:

bool(true)
bool(false)
bool(false)

Explanation:

As both $language1 and $language2 variables are declared and have values, so the isset() function returns TRUE (line 4). Though both $language1 and $language2 variables are declared and have values but variable $language3 doesn’t exist, so the isset() function returns FALSE (line 9). Though $language1, $language2 $language3 variables are declared but the first two variables have values and variable $language3 has a value of NULL, so the isset() function returns FALSE (line 15).

Example 4:

<?php
$employeeOne = array (
                'name' => "John Doe",
                'Sex' => "Male",
                'Age' => 30);
var_dump(isset($employeeOne["name"]));
echo "<br />";
var_dump(isset($employeeOne["designation"]));
?>

Output:

bool(true)
bool(false)

Explanation:

As the “name” key exists in the $employeeOne array, isset() function returns TRUE (line 6). But, “designation” keydoesn’t exist, so, isset() function returns FALSE (line 8).

Example 5:

<?php
class car{
    public $brand = "Toyota";
    public $color = "White";
}

$carObject = new car();
var_dump(isset($carObject -> brand));
echo "<br />";
var_dump(isset($carObject -> mileage));
?>

Output:

bool(true)
bool(false)

Explanation:

As the “brand” property exists in the car class, isset() function returns TRUE (line 8). But, “mileage” property doesn’t exist, so, isset() function returns FALSE (line 10).

Practical Usages of isset() Function:

  • You can use isset() function to check whether a variable is already declared with a value in your script so that your code doesn’t mess.
  • The function ensures you that you’re not trying to use any variable that doesn’t exist.

Notes on isset() Function:

  • Beside scalar types of data, you can also use this function on array, object type data etc. Check example 4.

PHP Version Support:

PHP 4, PHP 5, PHP 7, PHP 8

Summary: PHP isset() Function

isset() is a frequently used function, a built-in variable handling function, that is used to check whether a variable is defined and contains any value other than NULL.

Reference:

https://www.php.net/manual/en/function.isset.php