When programming, it can be important to check if a variable has been declared in memory and if it has a value before using the variable. This may seem unnecessary because a developer can just declare a variable and assign it a value before using the variable. However, as developers we often need to use variables with assigned values coming from outside sources from the specific code we are writing. Examples of outside sources to receive variables from are an API, data POSTed from a form, query parameters from a URL, or results from a SQL query.
The below screenshot shows an attempt to use an undefined variable in a PHP shell by echoing it. The result is a “PHP Notice: Undefined variable” error is displayed.
In PHP, isset() is a language construct that is used to check if a variable has been declared and that the variable’s value is not NULL.
Syntax: isset($var1 [, $var2 ...]);
Output: isset() will return FALSE when checking a variable that has not been declared or if the variable has a value of NULL. isset() will return TRUE if the variable has been declared and has a value other than NULL.
Using Multiple Parameters: Multiple variables can be checked with isset() by separating them with commas. When using isset() to check multiple parameters TRUE, will only be returned if all variables are considered set.
Results Of isset()
The PHP interactive shell can be used to quickly see if the results of using isset() are true or false. The PHP interactive shell can be accessed by typing php -a at the command line.
Result Of Checking A Defined Variable With isset()
The below screenshot from a PHP interactive shell shows a variable named $str being declared and assigned the string “Hello World”. The screenshot then shows var_dump() being used to display the data type and value of the result of isset($str).
Result Of isset($str): true
The $str variable has been declared and has a value other than NULL so true is returned.
Result Of Checking An Undefined Variable With isset()
The below screenshot shows var_dump() being used to display the data type and value of the result of isset($undeclared). In this example, the $undeclared variable has not been declared to the PHP interpreter before attempting to be used.
Result Of isset($undeclared): false
The $undeclared variable has not been declared to the PHP interpreter so false is returned.
Result Of Checking An Unset Variable With isset()
The below screenshot shows a variable named $str being declared and assigned the string “Hello World”. The screenshot then shows var_dump() being used to display the data type and value of the result of isset($str). The unset() function is then used to destroy the $str variable. The screenshot then shows var_dump() being used to display the data type and value of the result of isset($str).
Result Of isset($str) After unset(): false
Running unset($str) destroys the $str variable so at this point in the run time of the PHP application the $str variable is undeclared in memory so false is returned.
Result Of Checking NULL Variable With isset()
The below screenshot shows a variable named $var being declared and then assigned the value null. The screenshot then shows var_dump() being used to display the data type and value of the result of isset($var).
Result Of isset($var): false
The isset() function checks if the variable is declared. In this case it is. isset() then checks the variable’s value. This variable has a value of NULL so the variable is not set in PHP and false is returned.
Result Of Checking Array Offset With isset()
The below screenshot shows a variable named $arr being declared and assigned an array of [0, 1, 2]. The screenshot then shows var_dump() being used to display the data type and value of the result of isset($arr[3]).
Result Of isset($arr[3]): false
The indexed array has offsets declared and values assigned for $arr[0], $arr[1], and $arr[2]. The offset for $arr[3] is not declared so false is returned.
Results Of Checking Properties Of An Object With isset()
The below screenshot shows a class Person being created with properties $fName and $lName. No values are assigned to the properties when the class is created. The $person variable is then declared and assigned an instance of the Person class. The screenshot then shows var_dump() being used to display the data type and value of the result of isset($person->fName).
Result Of isset($person->fName): false
The instance of the Person class has a property fName that has been declared, but no value has been assigned to it. The value of the fName property is actually NULL so false is returned.
The below screenshot shows the same $person variable having the fName property set to the string “John”. The screenshot then shows var_dump() being used to display the data type and value of the result of isset($person->fName).
Result Of isset($person->fName): true
The instance of the Person class has the string “John” assigned to the property fName. The property is declared and it has a value different than NULL so true is returned.
Conclusion
The isset() language construct is used to determine if a variable is declared and if the variable’s value is different than NULL. It is important to check if a variable is set when data is coming from outside sources such as an API, data POSTed from a form, or query parameters from a URL. The above examples show the results of using isset() to determine if variables are considered set by PHP.
References:
https://www.php.net/manual/en/function.isset.php
https://www.php.net/manual/en/function.unset.php
https://kunststube.net/isset
https://davidwolfpaw.com/when-to-use-isset-empty-and-is_null-in-php
https://www.youtube.com/watch?v=Cg0S8VgOrxY