PHP isset function is used in forms to check whether the variables are set or not. If the variables are set, the isset function allow the form to get submitted and you will get the data on your email or other form action URL.
What Is PHP isset Function
The PHP isset function is used to check whether the PHP variable is set or not. If the variable is null, PHP isset function returns a false value. But, if the variable is not null and empty, it returns a true value.
Again if you pass multiple variables to the PHP isset function, then if all the variables are not null, the function returns a true value. The function returns a boolean value as the result of the output of the function.
Syntax
1 2 3 |
<?php isset(); ?> |
Reason Behind Using The function with Forms
Use this function when you want to execute the code only when some variable contains some value. This can be useful when you want to check the true condition of a variable and execute a code after it returns a boolean true or false value.
If you want to execute code on form submit, you have to first check whether the user clicks the submit button or not. If the user submits the button and fills all the form fields, then execute the PHP code, if not, don’t execute code. For that, you have to use the PHP isset function.
Example: Suppose the form tag contains the name, email input field and a submit button and there are some PHP codes. You want to execute the PHP codes only when the user fills all the fields and click the submit button. PHP isset function can help you do that and you can find more detail of function here. It will not allow executing the PHP code if the user does not click submit button and fills all their data.
Example
1 2 3 4 5 |
<form method="post"> <input name="name" type="text" placeholder="Name" /> <input name="email" type="text" placeholder="Email" /> <input name="submit" type="submit" value="submit" /> </form> |
and there is some PHP code to execute on form submission.
1 2 3 4 5 6 7 |
<?php if(isset($_POST['submit'])) { $name = $_POST['name']; $email = $_POST['email']; echo "The form has submitted and the name is $name and the email is $email."; } ?> |
Now, suppose the user puts the name as Roshan and email as [email protected]. Then the final output on form submission will be as given below.
Output
This PHP code prints the output using the PHP echo function.
How to Use PHP Isset Function To Check if Variable is set or Not
Now, let us take a variable var1 and execute a code only when the value of the variable var1 is not null. You have to use IF condition inside which you use PHP isset function. If the condition is true, the code will be executed. Check the code to know how to use isset function in PHP form submit to check whether a variable is set or not.
Example
1 2 3 4 5 |
<?php if(isset($var1)){ echo "The given variable is not null."; } ?> |
Output
If you use unset function and check the variable with PHP isset function. The output results in a false value which in results does not execute the code.
Here, we set a variable value as $var = 5 and unset it again using the unset function. Then after check, if the variable is set or not. After you unset the variable, the output returns the false value and the code will not get executed.
1 2 3 4 5 6 7 8 9 |
<?php $var = 5; unset($var); if(isset($var)){ echo "The given variable is not null."; }else{ echo "The given variable returns a null value and your does not gets executed."; } ?> |
Output
The above example showing the output which returns null value as the variable is not set. It uses PHP unset()
to remove the assigned value to the variable.
You may also like to read
- Ajax Image Upload Using PHP, JQuery With Image Preview
- PHP Function: How To Declare A Function In PHP?
Hope, you like the tutorial of how the isset function used to check if the variable is set or not. If you have any query regarding the tutorial, please comment below.
Can u help me with my code actually my if statement is not executing
Sure, Please provide your code here.