PHP isset and unset with 5 examples and code
Purpose of isset in PHP
The isset function is used to check if a variable is set or not.
That means it determines if a variable is assigned a value and is not null.
Also, the isset PHP function checks if the given variable is not unset by using the unset function.
Syntax of using the isset function
This is how you can use the isset function:
1 |
$bool_var = isset($varuable_to_check); |
The PHP isset function returns true if a variable is set and false if it is not or being unset by using the unset function.
Example of using isset in PHP
In this example, we have simply declared a string variable and assigned it “” value. Although it does not contain any character, but as it is being set, see the output by clicking the image or link below:
See online demo and code
You can see, the output is “The variable is set” although it contains zero character. The isset function will return as true because we had set it.
Example without setting a variable
In that example, we will not assign it any value, not even “”. We have just declared that string variable and see the output:
See online demo and code
So that resulted in the “The variable is not set”, even we had declared it. Now look at an example of isset with 2 variables.
isset example with two variables
As mentioned earlier, you can specify one or more variables in the isset function. However, if any variable is not set or had been sunset by using the unset function or it is a Null, it will return as false. In that case even other variables are set, the function will still return the false.
See the following example and output:
See online demo and code
You see although we have set the first variable while the second variable was only declared and not given any value. The isset function returned false with the output:
“The variables are not set”
An example with array elements
Not only you can use isset function with simple variables to determine if those are set or not but you can also use it to check array elements as well.
In the following example, an associative array of three elements is created. The first and third elements are assigned values while the second element is set to null.
Then we used a foreach loop to iterate through the array elements. Inside the foreach loop, the isset function is used to check if the current element is set or null. If that is set, the true message will be shown otherwise false along with array key and element values.
See the example online with code by following the link or image:
See online demo and code
Syntax of unset function
Following is the general syntax to use PHP unset function:
1 |
unset($variable_to_unset); |
You may specify one or more variable to be destroyed by using the unset in a single call.
An unset example
In this example, a variable was declared and assigned a value. After that, it was unset by using the unset function, so it was destroyed.
See the code and output before and after using the unset function.
See online demo and code
Why we use isset and unset functions
There are a few solid reasons to use the isset function in PHP programs. One of the reasons is, while you use a lot of variables in your PHP programs, it is generally a good practice to check variables if those are set or not before using those.
Basically, it checks whether a variables or index exists or not. PHP generates notice if you think a variable exists while actually it is not. By using the PHP isset function, you may avoid any unwanted notices.
The unset PHP function
The unset function of PHP is used to destroy a given variable.
How to use PHP isset function
You have to specify a variable to set inside the isset function. As isset returns a Boolean value (true or false), you can assign it to a Boolean variable as shown below:
1 |
Bol_variable = isset($variable_to_set_value); |
You may specify more than one variable in the isset function. In that case, the isset function will return true if all variables are set otherwise it will return false.
1 |
Bol_variable = isset($variable_1, $Variable_2….); |
Also note, if you have used unset function to destroy a variable, it cannot be set by using the isset function.
The examples are shown in the above section.
Using unset function
The unset function is quite simple to use. Simply specify variable(s) name inside the unset function as follows:
1 |
unset ($variable1, $variable2…); |
A few points about the unset function:
- The scope is important while destroying a variable by PHP unset function.
- If a global variable is unset inside a function, it will be destroyed in that function only. It will retain the value outside.
- The unset function does not return any value.