John Mwaniki /   18 May 2023

PHP unset() Function: A Comprehensive Guide

As a PHP developer, you most probably have used or come across the unset() function but you may not have much understanding about it.

In this article, we will delve into the PHP function, what it does, and everything you need to know about it.

unset() is a built-in PHP function that destroys the specified variable(s).

Syntax

unset($variable)

The unset() function accepts one or more parameters, which can be variables or array elements, and removes them from memory. It does not return any value.

How the unset() function works

When the unset() function is called, it destroys the specified variable or array element, freeing up memory occupied by it. This action essentially removes the variable from the current symbol table.

In the case of an array element, unset() removes the element from the array while keeping the remaining elements intact. However, it is important to note that the keys in the array will not be reindexed automatically.

Example 1

Using the unset() function with one variable parameter.

<?php
$fruit = "Apple";
echo "I love ".$fruit." fruit<br>";
unset($fruit);
echo "I love ".$fruit." fruit";
?>

Output:

I love Apple fruit
I love fruit

In the second line of output, the fruit name (Apple) is missing after unsetting the variable $fruit.

Additionally, you get the PHP warning below if you have enabled error reporting. This is because PHP no longer recognizes the variable after destroying it and it's the same as an undefined variable.

Warning: Undefined variable $fruit in /path/to/file.php on line 5

Example 2

Using the unset() function with more than one variable. Just separate them with commas.

<?php
$name = "John Doe";
$email = "johndoe@email.com";
$age = 30;
unset($email, $age);
echo "Name: ".$name."<br>Email: ".$email."<br>Age: ".$age;
?>

Output:

Name: John Doe
Email:
Age:

Example 3

Using the unset() function to remove an array element.

<?php
$students = array("Arthur", "Merlin", "Gaius", "Morgana", "Mordred");
print_r($students);
unset($students[2]);
echo "<br>";
print_r($students);
?>

Output:

Array ( [0] => Arthur [1] => Merlin [2] => Gaius [3] => Morgana [4] => Mordred )
Array ( [0] => Arthur [1] => Merlin [3] => Morgana [4] => Mordred )

In the above example, we have removed the third element of the array using its index '2'. You can see that "Gaius" is missing in the second print_r() statement.

Example 4

In a similar way to an indexed array, we can also use the unset() function with an array key to remove an element from an associative array.

<?php
$person = array("name" => "Frodo", "age" => 25, "occupation" => "Fisherman");
print_r($person);
unset($person["occupation"]);
echo "<br>";
print_r($person);
?>

Output:

Array ( [name] => Frodo [age] => 25 [occupation] => Fisherman )
Array ( [name] => Frodo [age] => 25 )

We have used the array key "occupation" to remove the last element from the array.

If you want to destroy the whole array instead of specific elements in it, you simply pass the array variable to the unset() function.

The unset() function behaves differently depending on the environment it's called in. When called inside a user-defined function, unset() only unsets variables locally within the function but not outside of it.

Example

In this example, we define a variable outside a user-defined function, then pass it as a parameter when calling the function. We then print the variable within the function, unset and print it within the function then print it again outside of the function after calling it.

<?php
function destroy_var($var) {
    echo "Var inside function: ".$var."<br>";
    unset($var);
    echo "Var inside function after unsetting: ".$var."<br>";
}

$var = "Hello World!";
destroy_var($var);
echo "Var outside function: ".$var;
?>

Output:

Var inside function: Hello World!
Var inside function after unsetting:
Var outside function: Hello World!

The variable $var gets unset and the effect applies only within the context of the destroy_var() function. Outside of the function, the variable $var retains the same value as before calling unset().

To unset a global variable inside of a function, we have to use the $GLOBALS array to do so.

<?php
function destroy_var() {
    unset($GLOBALS['var']);
}

$var = "Hello World!";
echo "Before unsetting: ".$var."<br>";
destroy_var();
echo "After unsetting: ".$var;
?>

Output:

Before unsetting: Hello World!
After unsetting:

Conclusion

The unset() function plays a crucial role in destroying variables and array elements. It's applicable in deleting unwanted data such as variables, array elements, form data, and sessions from the memory, thus freeing up memory resources, and optimizing the performance of your application.

In this article, we have explored how the unset() function works and how it behaves in different environments.