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 set if the variable is declared and the variable’s value is not equivalent to null.

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 isset method to check if a variable is set or not.

  • For the value 124 and '' the isset method returns true.

  • But for the value null, the isset method returns false.

  • We called the isset method with multiple variables as an argument to check if all the variables are declared, and the values are not equal to null.

Free Resources