PHP isset and unset Functions

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:

$bool_var = isset($varuable_to_check);

The PHP isset function returns true if a variable is set and false if it is not or is 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, as it is being set, see the coce and output below:

$str_isset = "";
$bol_isset = isset($str_isset);

If ($bol_isset){
   echo "The variable is set";
}
else {
    echo "The variable is not set";   
}

Result:

The variable is set

You can see, the output is “The variable is set” although it contains zero characters. 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:

$str_isset = "";
$bol_isset = isset($str_isset);

If ($bol_isset){
   echo "The variable is set";
}
else {
    echo "The variable is not set";   
}

Output:

The variable is not set

So that resulted in the “The variable is not set”, even though 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 if other variables are set, the function will still return the false.

See the following example and output:

$str_isset = "";
$str_2;
$bol_isset = isset($str_isset,$str_2);

If ($bol_isset){
   echo "The variables are set";
}
else {
    echo "The variables are not set";   
}

Output:

The variables are not set

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 as otherwise false along with array key and element values.

See the example code/output:

$salaries = array(); 
$salaries["Anna"] = 5000;
$salaries["John"] = null;
$salaries["Aamir"] = 10000;

foreach($salaries as $key_arr => $val_arr){
    if (isset($val_arr)){
    echo "(This is true/set) " . $key_arr . " = " .$val_arr ."<br />";
    }
    else{
        echo "(This is false/not set) " . $key_arr . " = " .$val_arr ."<br />";
    }
}

Output:

(This is true/set) Anna = 5000
(This is false/not set) John =
(This is true/set) Aamir = 10000

Syntax of unset function

Following is the general syntax to use PHP unset function:

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.

$str_isset = "The isset example";
$bol_isset = isset($str_isset);

If ($bol_isset){
   echo "The variable is set </BR>";
}
else {
    echo "The variable is not set </BR>";   
}
//Using unset function
unset ($str_isset);
$bol_isset = isset($str_isset);
If ($bol_isset){
   echo "The variable is set";
}
else {
    echo "The variable is not set";   
}

Output:

The variable is set
The variable is not set

Why do 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 they 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:

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.

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:

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.

 

Author - Atiq Zia

Atiq is the writer at jquery-az.com, an online tutorial website started in 2014. With a passion for coding and solutions, I navigate through various languages and frameworks. Follow along as we unravel the mysteries of coding together!