The isset() function is a built-in function of PHP, which is used to determine that a variable is set or not. If a variable is considered set, means the variable is declared and has a different value from the NULL. In short, it checks that the variable is declared and not null.
This function returns true if the variable is not null, otherwise it returns false. Note that the null character ("\0") is considered different from the PHP NULL constant.
isset (mixed variable, ….)
Mixed denotes that the parameter may be multiple types.
This function accepts one or more parameters, but it must contain atleast one parameter.
variable (required) - It is necessary to pass this parameter to this function because it holds that variable/element, which is to be checked.
… - More variables to be checked. These are optional to pass in this function.
The isset() function returns a boolean value: It may be either true or false.
If the variable exists and does not contain a NULL value, it returns TRUE otherwise, it returns FALSE.
From PHP 5.4.0, non-numeric offsets of strings return FALSE.
Below some list of examples are given through which you can understand isset() much better-
Example 1
<?php $var1 = 'test'; if(isset($var1)) { echo "The variable $var1 is set, so it will print. </br>"; var_dump(isset($var1)); } ?>
Output
Example 2: Difference between null character and NULL constant
The null character ("\0") is different from the PHP NULL constant. With the help of below example, you can practically see the difference.
<?php $x = 9; //TRUE because $x is set if ($resultX = isset ($x)) { echo "Variable 'x' is set."; } echo var_dump(isset($resultX)). "</br>"; $y = NULL; //False because $y is Null if (isset ($y)) { echo "Variable 'y' is not set."; } echo var_dump(isset($y)). "</br>"; $z ='\0'; //TRUE because \0 and NULL are treated different if ($resultZ = isset ($z)) { echo "Variable 'z' is set."; } echo var_dump(isset($resultZ)); ?>
Output
Example 3: Use of unset()
In this example, we will use the unset() function to unset the variable. Look at the below example:
<?php $var1 = 'test'; $var2 = 'another test'; if (isset($var1) && isset( $var2)) { echo "It will print because variables are set. </br>"; var_dump (isset($var1)); var_dump (isset($var2)); } unset ($var1); unset ($var2); echo "</br> </br>Variables after unset: </br>"; var_dump (isset($var1)); var_dump (isset($var2)); ?>
Output
Example 4: Difference between isset and !empty
The isset() and !empty() functions are operated same and both return same results. The only difference between them is that if the variable is not present, the !empty() function does not generate any warning.
<?php $valiable1 = '0'; //check the isset() function if (isset ($variable1)) { print_r($variable1 . " is checked by isset() function"); } echo "</br>"; $variable2 = 1; //check the !empty() function if (!empty ($variable2)) { print_r($variable2 . " is checked by !empty() function"); } ?>
Output
Example 5: Checking multiple variables
<?php $var_test1 = ''; $var_test2 = ''; if (isset ($var_test1, $var_test2)) { echo "Variables are declared and also not null."; } else { echo "Variables are not set or null"; } ?>
Output
Example 5: isset() to check session variable
<?php $user = 'rookienerd'; $_SESSION['userid'] = $user; if (isset($_SESSION['userid']) && !empty($_SESSION['userid'])) { echo " Session is available, Welcome to $_SESSION[userid] "; } else { echo " No Session, Please Login "; } ?>
Output