PHP isset function

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 Use PHP isset()

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 a 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. 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 info@tutorialdeep.com. 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 info@tutorialdeep.com.

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

Example of 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 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.

Reference

PHP.net isset function.
isset function needs.

Leave a Reply

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