PHP isset() – Check If Variable Is Set



PHP isset() function

PHP isset() function is used to check if a variable has been set or not.
This can be useful to check the submit button is clicked or not.
The isset() function will return true or false value. The isset() function returns true if variable is set and not null.

PHP isset() function syntax

isset(“variable”);

PHP isset() Example

<?php

$str = 'meera'; 
var_dump(isset($str));

?>

The output is :

boolean true

lets another php form example to understand more about isset() function.

Here, we are do sum of two values, design php form with two textbox and one button control like :

<html>
<head>
<title>PHP isset() example</title>
</head>
<body>

<form method="post">

Enter value1 :<input type="text" name="str1"><br/>
Enter value2 :<input type="text" name="str2"><br/>
<input type="submit" value="Sum" name="Submit1">

<?php
$sum=$_POST["str1"] + $_POST["str2"];
echo "The sum = ". $sum;
?>

</form>
</body>
</html>

When we run above php example while first time page load it will show error like below:

php isset
PHP isset() function example

 

In above code add isset() function code to remove above error while page loading.

<html>
<head>
<title>PHP isset() example</title>
</head>
<body>

<form method="post">

Enter value1 :<input type="text" name="str1"><br/>
Enter value2 :<input type="text" name="str2"><br/>
<input type="submit" value="Sum" name="Submit1"><br/><br/>

<?php
if(isset($_POST["Submit1"]))
{
$sum=$_POST["str1"] + $_POST["str2"];
echo "The sum = ". $sum;

}
?>

</form>
</body>
</html>

The output of isset() function php example is :

PHP isset() function example
PHP isset() function example

 

3 thoughts on “PHP isset() – Check If Variable Is Set

Leave a Reply

Your email address will not be published. Required fields are marked *