What is isset and unset in php?

❮ PHP Variable Handling Reference

Example

Check whether a variable is empty. Also check whether the variable is set/declared:

$a = 0;
// True because $a is set
if (isset($a)) {
  echo "Variable 'a' is set.
";
}

$b = null;
// False because $b is NULL
if (isset($b)) {
  echo "Variable 'b' is set.";
}
?>

Try it Yourself »


Definition and Usage

The isset() function checks whether a variable is set, which means that it has to be declared and is not NULL.

This function returns true if the variable exists and is not NULL, otherwise it returns false.

Note: If multiple variables are supplied, then this function will return true only if all of the variables are set.

Tip: A variable can be unset with the unset() function.


Syntax

Parameter Values

ParameterDescription
variable Required. Specifies the variable to check
... Optional. Another variable to check

Technical Details

Return Value:TRUE if variable exists and is not NULL, FALSE otherwise
Return Type:Boolean
PHP Version:4.0+
PHP Changelog:PHP 5.4: Non-numeric offsets of strings now returns FALSE

❮ PHP Variable Handling Reference


  • 1. Purpose of isset in PHP
  • 2. Example of using isset in PHP
  • 3. Example without setting a variable
  • 4. isset example with two variables
  • 5. An example with array elements
  • 6. Syntax of unset function
  • 7. An unset example
  • 8. Why we use isset and unset functions
  • 9. How to use PHP isset function
  • 10. Using unset function

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 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:

What is isset and unset in php?

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:

What is isset and unset in php?

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:

What is isset and unset in php?

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:

What is isset and unset in php?

See online demo and code

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.

What is isset and unset in php?

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:

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.

What is the use of isset and unset in PHP?

PHP isset() Function This function returns true if the variable exists and is not NULL, otherwise it returns false. Note: If multiple variables are supplied, then this function will return true only if all of the variables are set. Tip: A variable can be unset with the unset() function.

What is unset () in PHP?

unset() destroys the specified variables. The behavior of unset() inside of a function can vary depending on what type of variable you are attempting to destroy. If a globalized variable is unset() inside of a function, only the local variable is destroyed.

What is Isset PHP?

The isset() function is an inbuilt function in PHP which checks whether a variable is set and is not NULL. This function also checks if a declared variable, array or array key has null value, if it does, isset() returns false, it returns true in all other possible cases.

What is the difference between isset and empty in PHP?

The isset() function is an inbuilt function in PHP that is used to determine if the variable is declared and its value is not equal to NULL. The empty() function is an inbuilt function in PHP that is used to check whether a variable is empty or not.