We have seen about several variable functions available in PHP to work with variables. Among them, isset() is one of the widely used functions. isset() is used to check whether a given variable is set with a not NULL value.
PHP isset() returns TRUE if a given variable is set to any value including the empty string. And, it returns FALSE, if the variable is not initialized or has NULL.
Using the PHP isset() function, we can check multiple variables at the same time. In such a scenario, this function returns the boolean value TRUE, if and only if, all variables passed as its arguments are set. Or else, it will return FALSE.
While sending overloaded properties or variables as the arguments of isset(), then it triggers PHP magic method __isset() if defined.
bool isset($var1, $var2, ...);
PHP isset() accepts only variable references as its arguments and not any direct values. For example, if we pass string “direct value” to isset(), then it will cause PHP error,
Parse error: syntax error, unexpected '"direct value"' (T_CONSTANT_ENCAPSED_STRING) in
<?php
$var = "";
print "isset():" . isset($var) . "<br/>";
$var = "apple";
print "isset('apple'):" . isset($var) . "<br/>";
$var = NULL;
print "isset('NULL'):" . isset($var) . "<br/>";
$var = FALSE;
print "isset('FALSE'):" . isset($var) . "<br/>";
$var = 0;
print "isset('0'):" . isset($var) . "<br/>";
print "isset(undefined):" . isset($var3) . "<br/>";
?>
Output:
isset():1 isset(‘apple’):1 isset(‘NULL’): isset(‘FALSE’):1 isset(‘0’):1 isset(undefined):
In my next article, let us discuss about the difference between isset(), empty() and is_null()