PHP isset() Function
The PHP isset() function checks whether a variable is declared and is different than null. A variable is considered set, if the variable is declared and it is different than null.
If multiple parameters are supplied then this function returns true only if all of the parameters are considered set. It evaluate variables from left to right and stops as soon as an unset variable is found.
If a variable has been unset with the unset() function, it is no longer considered to be set.
This function returns false when checking a variable that has been assigned to null. Note that a null character ("\0") is not equivalent to the PHP null constant.
Syntax
isset(var, vars)
Parameters
var |
Required. Specify the variable to be checked. |
vars |
Optional. Specify the variable to be checked. Multiple parameters are allowed. |
Return Value
Returns true if var exists and has any value other than null. false otherwise.
Example: isset() examples
The example below shows the usage of isset() function.
<?php $a = null; $b = ''; $x = 'test'; $y = 10; var_dump(isset($a)); //returns: bool(false) var_dump(isset($x, $a)); //returns: bool(false) echo "\n"; var_dump(isset($b)); //returns: bool(true) var_dump(isset($x, $y)); //returns: bool(true) var_dump(isset($b, $x, $y)); //returns: bool(true) echo "\n"; //unset variable $x unset($x); var_dump(isset($x, $y)); //returns: bool(false) var_dump(isset($b, $x, $y)); //returns: bool(false) echo "\n"; //this will evaluate to TRUE so the text will be printed if (isset($y)) { echo "y is set to $y"; } ?>
The output of the above code will be:
bool(false) bool(false) bool(true) bool(true) bool(true) bool(false) bool(false) y is set to 10
Example: using isset() on arrays
The isset() function also works in the similar way on elements of an array. Consider the example below:
<?php $Arr1 = array(10, null); $Arr2 = array("a" => 10, "b" => null); //checking elements of $Arr1 var_dump(isset($Arr1[0])); var_dump(isset($Arr1[1])); var_dump(isset($Arr1[10])); echo "\n"; //checking elements of $Arr2 var_dump(isset($Arr2['a'])); var_dump(isset($Arr2['b'])); var_dump(isset($Arr2['z'])); ?>
The output of the above code will be:
bool(true) bool(false) bool(false) bool(true) bool(false) bool(false)
❮ PHP Variable Handling Reference