What is the isset method in PHP?
The isset method is used to check if the variables are declared and have a value other than null.
Syntax
isset(mixed $var, mixed ...$vars): bool
Parameters
var– This is the variable to be checked.vars– These are further variables.
Return value
This method returns true if the passed variables are not null and declared. Otherwise, it returns false.
If multiple variables are passed, all the variables should be considered a set for the function to return true. Otherwise, false will be returned.
A variable is considered a
setif the variable is declared and the variable’s value is not equivalent tonull.
Code
<?php$num = 123;printIsSet($num);$str = '';printIsSet($str);$nullval = null;printIsSet($nullval);echo "Passing multiple variables\n\n";echo "isset(num, str) : ";var_dump(isset($num, $str));echo "---------\n\n";echo "isset(num, str, nullval) : ";var_dump(isset($num, $str, $nullval));echo "---------\n\n";function printIsSet($test){echo "The Value is : " ;var_dump($test);echo "IsSet: ";var_dump(isset($test));echo "---------\n\n";}?>
Explanation
In the code above,
-
We have used the
issetmethod to check if a variable issetor not. -
For the value
124and''theissetmethod returnstrue. -
But for the value
null, theissetmethod returnsfalse. -
We called the
issetmethod with multiple variables as an argument to check if all the variables are declared, and the values are not equal tonull.