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.

bool isset (mixed $var1 [, mixed $var2… [, mixed $varN)

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.

<?php

//Checking for defined variable

$myVar1 = 10;

if(isset($myVar1))


    echo “The variable is defined.
;

else


    echo “The variable is undefined.
;


 

//Checking for undefined variable

$myVar2;

if(isset($myVar2))


    echo “The variable is defined.
;

else


    echo “The variable is undefined.
;


 

echo $myVar3;

Output:

The following output will appear after executing the above script.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image4-5.png" data-lazy- height="207" src="data:image/svg xml,” width=”1123″>

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.

<?php

//Assign a null value

$myVar = null;

var_dump(isset($myVar));

echo
;

//Assign 0 value

$myVar = 0;

var_dump(isset($myVar));

echo
;

//Make the variable undefined using unset() function

unset($myVar);

var_dump(isset($myVar));

?>

Output:

The following output will appear after executing the above script.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image5-5.png" data-lazy- height="159" src="data:image/svg xml,” width=”1112″>

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.

<?php

//Assign a string value

$myStr = ‘Linux Hint’;

//Check the value of the 6th position of the string value starting from 0

echo isset($myStr [6]);

//Check the value of the 13th position of the string value starting from 0

echo isset($myStr [12]);

?>

Output:

The following output will appear after executing the above script.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image8-3.png" data-lazy- height="138" src="data:image/svg xml,” width=”1115″>

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.

<?php

//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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image6-5.png" data-lazy- height="140" src="data:image/svg xml,” width=”1114″>

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.

<?php

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image3-5.png" data-lazy- height="129" src="data:image/svg xml,” width=”1116″>

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.

<?php

//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

{

?>

<html>

<body>

<form method=“post” action=“#”>

Enter the value of n1: <input type=“text” name=“n1” /><br/>

Enter the value of n2: <input type=“text” name=“n2” /><br/>

<input type=“submit” name=“submit” value=“Sum”>

</form>

</body>

</html>

<?php

}

?>

Output:

The following output will appear after executing the above script.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image1-8.png" data-lazy- height="174" src="data:image/svg xml,” width=”1115″>

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image2-6.png" data-lazy- height="155" src="data:image/svg xml,” width=”1117″>

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.

<?php

$var1 = “Linux”;

$var2 = True;

var_dump(isset($var1, $var2));

echo
;

$var1 = “Good”;

$var2 = 100;

$var3 = null;

var_dump(isset($var1, $var2, $var3));

echo
;

?>

Output:

The following output will appear after executing the above script.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/image7-3.png" data-lazy- height="162" src="data:image/svg xml,” width=”1114″>

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.

About the author

<img data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2022/04/echo/channel-logo-150×150.jpg6254e95f4f693.jpg" height="112" src="data:image/svg xml,” width=”112″>

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.