Home >>PHP Tutorial >PHP isset function
PHP isset() function is used to check if a variable exists in the code or not. It means we check the value of any particular variable. We use the isset function to check if any variable that is passed, exists in the code and also possess some value. If a variable have some value then it is said to be set and if it doesn't have any value stored in it that is contains NULL then it is said to be unset.
Syntax:- isset(variable);
Let's take an example of Isset:
<?php $a = 5; //variable 'a' is declared & defined if (isset($a)) { //it will return true or false echo "True"; //print True if isset return the True value } Else{ echo "False"; //print False if isset return the False value } ?>
It returns the result as Boolean. If we pass any variable in the isset function, it returns the result as either True or False. If the variable that we have passed has been declared and also contain some value other than NULL then it will be returning True as the result but if the variable is not declared or defined in the code that means either there is no such variable present in the code or it has a NULL value then it will return False as the result. We can check multiple variables at a time by passing multiple variables in the Isset(). In this case, function will check each and every variable whether it is set or unset and if all the variables are set then it will return True and if any of the variable is unset then it will return False.
Syntax :- isset(variable1, variable2, variable3,…..);
Let's take an another example of Isset:
<?php $a = 0; // True because variable 'a' is set if (isset($a)) { echo "Variable 'a' is set.<br>"; } else { echo "Variable 'a' is unset.<br>"; } $b = null; // False because variable 'b' is NULL if (isset($b)) { echo "Variable 'b' is set.<br>"; } else { echo "Variable 'b' is unset.<br>"; } if (isset($c)) { echo "Variable 'c' is set.<br>"; } //False because variable 'c' is not declared else { echo "Variable 'c' is unset.<br>"; } ?>
Let's take an example of passing multiple variables in isset:
<?php $a=5; $b=6; $c=NULL; //False because 'c' is NULL if(isset($a,$b,$c)){ echo "All the variables are set."; } else{ echo "Any or All variables are Unset."; } ?>
Isset() is used in forms for the validation purpose where we can check whether a variable is set or unset in the form data.
Let’s see how can we use isset function in forms:
<?php if(isset($_POST[‘submit’])) { echo (“Name:”.$_POST[‘name’]. “<br>”); echo(“College:”$_POST[‘clg’]. “<br>”): } ?> <HTML> <head> <title>Isset in Form</title> </head> <body> <form method= “POST” Action= “#”> <p> Name: <input type = “text” name= “name”/> </p> <p> College: <input type = “text” name= “clg”/> </p> <input type = “submit” name = “submit” Value = “submit”> </body> </HTML>