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
$x="some value"; // assigned a string to the variable $x here
if(isset($x)){ // checking the existence of the variable by isset
echo "The variable exists";
	echo " Variable does not exists ";}

filter_has_var to check variable

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
if(isset($test_var)){echo " Output : True";}
echo "Output : False ";
Output is
Output : False
In the above code, Let us change the variable like this
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.
if(isset($test_var1,$test_var2)){echo " Output : True";}
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.

Using isset to check array variable

$my_array=array("first"=>'test', "second"=>34, "third"=>'');

var_dump(isset($my_array['first'])); // bool(true)
var_dump(isset($my_array['second'])); // bool(true)
var_dump(isset($my_array['new_one'])); // bool(false)

How to create PHP Array

Checking elements with NULL value

$input=array('One' =>1,'Two'=>null);
echo var_dump(isset($input['Two'])); // bool(false)
If the element is equal to NULL then output became False, however array_key_exists() returns True.
$input=array('One' =>1,'Two'=>null);

if(array_key_exists('Two',$input)){ // True 
echo "Key is available ";	// this is the output
echo "Key is NOT available ";	
echo "<br>";
echo var_dump(isset($input['Two'])); // bool(false)

Using isset() with session variable

if(isset($_SESSION['userid']) && !empty($_SESSION['userid'])) {
 echo " session  is available, Welcome $_SESSION[userid] ";
 echo " No Session , Please Login ";

Read more on Session Variable

Declaring variables Cookies to store Infomation Getting Type of Variable var_dump() empty()
Subscribe to our YouTube Channel here


* indicates required
Subscribe to plus2net

    Chris Kavanagh


    Very Nice Explanation. This really cleared it up for me. Thank You.
    masroor javed


    thnks for help,,,,,

    Post your comments , suggestion , error , requirements etc here

    PHP video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2023 All rights reserved worldwide Privacy Policy Disclaimer