How to check whether the variable is present or not in php ?

(Last Updated On: August 11, 2016)

Checking variable presence in php:

isset():

isset() is a function used to identify whether the variable is present or not in the php programs.
It will take the variable name as argument and returns the boolean results(true/false).

PHP Program to check variable presence in the code:

<?php
$var1 = 10;
if(isset($var1)){
echo '$var1 Variable is there'."\n";
}else{
echo '$var1 Variable is not there'."\n";
}
if(isset($var2)){
echo '$var2 Variable is there'."\n";
}else{
echo '$var2 Variable is not there'."\n";
}
?>

Output:

$var1 Variable is there
$var2 Variable is not there

Note:
In the program var1 is exist, so its printed variable var1 is there and var2 is not exist, so its printed var2 variable is not there.

empty() function is the opposite to isset() function. It will return true if the variable is not there/the variable value is empty/null.

PHP Recommended Books:

206 total views, 0 views today

Leave a Reply

Your email address will not be published. Required fields are marked *