Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

isset
php
communitycreator

What is the isset method in PHP?

Programming Bytes

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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.

RELATED TAGS

isset
php
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring