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

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 gutter=”false”]
<?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";
}
?>
[/php]

Output:
[plain gutter=”false”]
$var1 Variable is there
$var2 Variable is not there
[/plain]

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:

Leave a Reply