How PHP isset Function Used To Check If Variable Is Set

PHP isset function

To get user input data from forms using PHP, you have to use PHP isset function to check whether the variables are set or not. If the variables are set and the user input all the required data. The forms 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

Why We Should Use PHP isset() 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.

and there is some PHP code to execute on form submission.

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

The form has submitted and the name is Roshan and the email is [email protected]

This PHP code prints the output using the PHP echo function.

How to Check if the Variable is set or not in Forms Using PHP

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 in PHP to check whether a variable is set or not.

Output

The given variable is not null.

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.

Output

The given variable returns a null value and does not get executed.

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

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.

Join the Conversation

2 Comments

Leave a comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.