PHP isset()

We have seen about several variable functions available in PHP to work with variables. Among them, isset() is one of the widely used function. 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 with any value including empty string. And, it returns FALSE, if the variable is not initialized or having NULL.

php_isset

Using PHP isset() function, we can check multiple variables at the same time. In such scenario this function returns 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.

PHP isset() Syntax

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 isset() Example

<?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()

This PHP code tutorial was published on October 3, 2013.

↑ Back to Top