Isset Function in PHP

Introduction

In this article I explain the isset() function in PHP. The isset() function determins if a variable is set. If a variable has been unset using the unset() function then it will no longer be set. If multiple parameters are supplied to the isset() function then it will return true only if all parameters are set. If the parameter is null then the isset() function returns false. You can use one or more parameters. The isset() function will return "true" only if all of the parameters or variables are set in your program.

Syntax

isset($var1,$var2..);

 

The function determins if a variable is set an returns output that is not null. In this function, you can pass one or two parameters for checking variable(s) as being set.

 

Parameter  
Var name The variable to be checked.

When the variable is exist then the returned output is true otherwise false.

Example1

This example also works for elements in an array.

<?php

$I = '';

if (isset($I))

{

    echo "This var is set so I will print.";

}

$x = "vinod";

$y = "anothervinod";

var_dump(isset($x));     

var_dump(isset($x, $y));

unset ($x);

var_dump(isset($x));   

var_dump(isset($x, $y));

$var = NULL;

var_dump(isset($var));  

?>

Output
Isset() function in PHP.jpg
Example2

<?php

$x = array ('ram' => 1, 'here' => NULL, 'fruits' => array('x' => 'orange'));

var_dump(isset($x['ram']));           

var_dump(isset($x['foo']));            

var_dump(isset($x['hello']));          

var_dump(array_key_exists('here', $x));

var_dump(isset($x['fruits']['x']));       

var_dump(isset($x['fruits']['x']));       

var_dump(isset($x['cake']['x']['x']));

?>

Output

 Isset() function2 in PHP.jpg


Similar Articles