It is necessary to check any variable is defined or not defined before reading the file’s content because if the variable is undefined, it will generate an error when the value of that variable is tried to read. This problem can be solved by using PHP’s isset() function. It returns true if the variable is defined and false if it is undefined. Different uses of the isset() function have been shown in this tutorial.
Syntax:
One or more variables can be checked using the isset() function. The first argument of this function is mandatory, and other arguments are optional. The return type of this function is Boolean. The syntax of the isset() function is given below.
Different examples of isset() function:
The isset() function uses have been shown by using different examples in this part of the tutorial.
Example-1: Checking a simple variable using isset()
It is necessary to check a variable is defined or undefined before using the values of the variable in the script. Create a PHP file with the following script to know the use of the isset() function for a defined and an undefined variable. The first isset() function has been used to check the variable named $myVar1 that is defined. The second isset() function has been used to check the variable named $myVar2 that is undefined. Next, an undefined variable named $myVar3 has been printed without using the isset() function to check the output of the undefined variable.
//Checking for defined variable
$myVar1 = 10;
if(isset($myVar1))
echo "The variable is defined.<br/>";
else
echo "The variable is undefined.<br/>";
//Checking for undefined variable
$myVar2;
if(isset($myVar2))
echo "The variable is defined.<br/>";
else
echo "The variable is undefined.<br/>";
echo $myVar3;
Output:
The following output will appear after executing the above script.
Example-2: Checking the output of isset() using var_dump()
The output of the isset() function has been shown by using the var_dump() function in the following example. Create a PHP file with the following script to check the output of the variable that contains null or 0 or undefined value. The first var_dump() function will display the output for the null value. The second var_dump() function will display the output for the 0 value. The third var_dump() function will display the output of the undefined value.
Output:
The following output will appear after executing the above script.
Example-3: Checking the value of the particular index of a string
Create a PHP file with the following script to check the output of the isset() function for the valid and invalid index value of the string array. According to the script, the 6th position contains a value, but the 12th position does not contain any value.
Output:
The following output will appear after executing the above script.
Example-4: Checking the value of an array variable using isset()
Create a PHP file with the following script to check the value of the particular index of an array is defined or undefined by using the isset() function. An associative array has been declared in the script where the key contains the ID value, and the value includes the mark. The value of the particular index will be printed if the isset() function will return true; otherwise, a message will be printed.
//Declare an associative array
$marks = ['011189' => 78, '011156' => 99, '011134' => 75, '011181' => 81, '011112' => 60];
//Check the particular index of the array is defined or undefined
if (isset($marks['011156']))
echo "The marks of the student is ". $marks['011156'];
else
echo "The student id does not exist.";
?>
Output:
The following output will appear after executing the above script.
Example-5: Checking the value of $_GET[] variable using isset()
Create a PHP file with the following script to know the use of the isset() function for checking the particular index value of the $_GET[] variable.
if(isset($_GET['name']))
echo "The name of the person is ". $_GET['name'];
else
echo "No name is given";
?>
Output:
The following output will appear after executing the above script.
Example-6: Checking the value of $_POST[] variable using isset()
Create a PHP file with the following script to know the use of the isset() function for checking the particular index value of the $_POST[] variable. In the script, the first isset() function has been used to check the submit button has been pressed or not. Next, two isset() functions have been used to check the text fields are empty or not. If the submit button is pressed after taking two numeric values, the sum of the numeric values will be printed.
//Check the submit button is pressed or not
if(isset($_POST['submit']))
{
//Check the values of text fields
if(!isset($_POST['n1']) || !isset($_POST['n1']))
echo "Any of the field is empty.";
else
{
$num1 = (int)$_POST['n1'];
$num2 = (int)$_POST['n2'];
$result = $num1 + $num2;
echo "The sum of $num1 and $num2 is $result";
}
}
else
{
?>
Output:
The following output will appear after executing the above script.
Example-7: Checking multiple variables using isset()
Create a PHP file with the following script to know the use of the isset() function for checking multiple variables.
Output:
The following output will appear after executing the above script.
Conclusion:
The ways of using the isset() function for different purposes have been shown in this tutorial by using multiple examples. The most common uses of this function have been explained here to help PHP users use it properly in their scripts.