PHP isset function statement script and example

Some time we have to check the existence of a variable before using it for our further processing. The variable can be checked by using PHP isset function and the return value will be either true or false. Here is an example

<?Php
$x="some value"; // we have assigned a string to the variable $x here
if(isset($x)){        // checking the existence of the variable by using isset function
echo "The variable exists";} 
else{ echo " Variable does not exists ";}
?>

The code above will test the existence of the variable $x and since we have already declared it at first line so the PHP IF condition will return true and the statement inside if condition will be evaluated. 

Variable is Null

If the variable is set to Null then isset will return FALSE. Here is the code

<?Php
$test_var=null;

if(isset($test_var)){echo " Output : True";}
else{
echo "Output : False ";
}
?>

Output is

Output : False

In the above code, Let us change the variable like this

$test_var='';

The output will change to

Output : True

Checking more than one variable

If we check more than one variable then we will get out True if all the variables are True.

<?Php
$test_var1='';
$test_var2='';

unset($test_var2);

if(isset($test_var1,$test_var2)){echo " Output : True";}
else{
echo "Output : False ";
}
?>

By using unset() function we have removed one variable hence the output of isset function will be FALSE

empty() to check variable



This function is used in PHP LOGIN SCRIPT   and PHP LOGIN LOGOUT script to check the existence of  session variables.

Number of User Comments : 2


Google+
Chris Kavanagh10-02-2012
Very Nice Explanation. This really cleared it up for me. Thank You.
masroor javed30-04-2013
thnks for help,,,,,

Post Comment This is for short comments only. Use the forum for more discussions.




HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us

©2000-2015 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer