The isset() function checks if the variable is defined and not null.
In PHP, the isset()
function is used to determine whether a variable is set and is not NULL. It returns true if the variable exists and has a value other than NULL. Otherwise, it returns false.
Here’s a simple explanation:
$var = "Hello";
// isset() returns true because $var is set and has a value
if (isset($var)) {
echo '$var is set.';
} else {
echo '$var is not set.';
}
// Unset the variable
unset($var);
// isset() returns false because $var is unset
if (isset($var)) {
echo '$var is set.';
} else {
echo '$var is not set.';
}
Output:
$var is set.
$var is not set.
In an interview setting, you could explain the purpose and usage of isset()
, along with providing examples like the one above to illustrate its behavior.