Q:

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

0

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

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Use the PHP isset() function

You can use the PHP isset() function to test whether a variable is set or not. The isset() will return FALSE if testing a variable that has been set to NULL.

Let's check out an example to understand how this function basically works:

<?php
$var1 = '';
if(isset($var1)){
    echo 'This line is printed, because the $var1 is set.';
}
echo "<br>";
 
$var2 = 'Hello World!';
if(isset($var2)){
    echo 'This line is printed, because the $var2 is set.';
}
echo "<br>";
 
// Unset the variable
unset($var2);
 
if(isset($var2)){
    echo 'This line is printed, because the $var2 is set.';
} else{
    echo 'This line is printed, because the $var2 is not set.';
}
echo "<br>";
 
$var3 = NULL;
if(isset($var3)){
    echo 'This line is printed, because the $var3 is set.';
} else{
    echo 'This line is printed, because the $var3 is not set.';
}
?>

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

PHP  and MySQL Frequently Asked Questions

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
How to check whether a variable is empty in PHP... >>
<< How to find string length in PHP...