How to check whether a variable is set or not in PHP

The isset() function is an built-in function in PHP which is used to check whether a variable is set or not. The isset() will return true if testing a variable is set and not null.

Example:

<?php
$var1 = '';
if(isset($var1)){
echo '$var1 is set.'; // Output: $var1 is set.
}
echo "<br>";

$var2 = 'PHP Tutorials';
if(isset($var2)){
echo '$var2 is set.'; // Output: $var2 is set.
}
echo "<br>";

// Unset the variable
unset($var2);

if(isset($var2)){
echo '$var2 is set.';
} else{
echo '$var2 is not set.'; // Output: $var2 is not set.
}
echo "<br>";

$var3 = NULL;
if(isset($var3)){
echo '$var3 is set.';
} else{
echo '$var3 is not set.'; // Output: $var3 is not set.
}
?>

Praveen Kumar

I am a fullstack web developer with a passion for SEO, creating stunning websites, and organic marketing. I am a perfectionist.

Post navigation