How to Check If a Variable is Set with the PHP isset() Function

09/04/2021

Contents

In this article, I will introduce how to check if a variable is set with the PHP isset() function.

PHP isset() Function

The isset() function returns true if the variable exists and is not NULL, false otherwise.

Syntax:

isset($variable, ...);

You can use this function to check whether the required information such as name and address has been entered in the input form, and whether the checkbox is checked.

Sample

Sample Code

<?php
$a = 0;
if (isset($a)) {
  echo "Variable 'a' is set.";
} else {
  echo "Variable 'a' is not set.";
}

$b = "";
if (isset($b)) {
  echo "Variable 'b' is set.";
} else {
  echo "Variable 'b' is not set.";
}

$c = null;
if (isset($c)) {
  echo "Variable 'c' is set.";
} else {
  echo "Variable 'c' is not set.";
}
?>

Result

Variable 'a' is set.
Variable 'b' is set.
Variable 'c' is not set.