unset()
The unset() function is a built-in function in PHP that destroys a variable or an element of an array. It can be used to free up memory and remove variables or
Introduction
The unset() function is a built-in PHP function that destroys a variable or an element of an array. Once a variable is unset, it no longer exists — referencing it again behaves exactly as if it had never been declared.
This page covers the syntax of unset(), how it behaves with arrays, references, object properties, and variables inside functions, and how it differs from simply assigning null. Knowing these distinctions helps you write predictable code and avoid the most common unset() gotchas.
A key point to understand up front: unset() removes the name (the variable binding), not necessarily the underlying value. PHP frees the value's memory automatically through reference counting and its garbage collector when nothing else points to it — not at the exact moment you call unset().
Syntax
unset(mixed $var, mixed ...$vars): voidThe function takes one or more variables (separated by commas) and returns nothing (void). Each argument is the variable or array element you want to destroy.
<?php
$a = 1;
$b = 2;
$c = 3;
unset($a, $b, $c); // destroy several variables in one call
?>Removing an array element
The most common use of unset() is deleting an element from an array by its key:
Example of PHP unset()
Here we remove the element at index 1 ("banana"). The output is:
Array
(
[0] => apple
[2] => cherry
)Notice that unset() does not re-index the array — the remaining keys stay 0 and 2, leaving a "gap". This is the most frequent surprise with unset(). If you need a clean, sequentially re-indexed list (0, 1, 2, …), pass the result through array_values():
<?php
$array = ["apple", "banana", "cherry"];
unset($array[1]);
$array = array_values($array);
print_r($array);
?>Array
(
[0] => apple
[1] => cherry
)Destroying a standalone variable
When called on a plain variable, unset() removes it entirely. After that, the variable is treated as undefined:
<?php
$name = "John";
unset($name);
echo $name ?? "Variable is unset"; // Outputs: Variable is unset
?>The null coalescing operator ?? is the safe way to read a possibly-unset variable, because it does not emit an "Undefined variable" warning. Using isset() is another common way to check whether a variable still exists.
unset() vs. assigning null
These two look similar but are not the same:
<?php
$x = 5;
$y = 5;
unset($x); // $x no longer exists
$y = null; // $y still exists, its value is null
var_dump(isset($x)); // bool(false) — the variable is gone
var_dump(isset($y)); // bool(false) — isset() returns false for null too
var_dump(array_key_exists('y', get_defined_vars())); // bool(true) — $y is still defined
?>Use unset() when you want the variable (or array key) to not exist. Assign null when you want to keep the variable but clear its value. Note that isset() returns false in both cases, so to tell them apart you must check for the key's existence directly. See is_null() and empty() for related checks.
unset() inside a function
The behavior of unset() depends on the variable's scope, and this trips up many developers:
<?php
function destroy_local() {
$value = 10;
unset($value); // only the local copy is destroyed
echo $value ?? "local is unset"; // Outputs: local is unset
}
destroy_local();
?>When you unset() a variable that was passed by reference, only the local reference is broken — the caller's variable is untouched:
<?php
function destroy_ref(&$ref) {
unset($ref); // breaks the local binding, not the original
}
$data = "keep me";
destroy_ref($data);
echo $data; // Outputs: keep me
?>If you genuinely need a function to remove a variable from the calling scope, pass it as a referenced array element or use a global — but in most cases redesigning to return a value is cleaner. For more on scope rules, see Variables Scope.
Unsetting object properties
unset() also removes a property from an object instance:
<?php
$user = new stdClass();
$user->name = "Ann";
$user->role = "admin";
unset($user->role);
var_dump(isset($user->role)); // bool(false)
print_r($user);
?>bool(false)
stdClass Object
(
[name] => Ann
)The foreach reference gotcha
A classic bug: after iterating an array with a reference (&$value), the reference variable still points to the last element. Reusing it later silently corrupts the array. The fix is to unset() the reference right after the loop:
<?php
$items = [1, 2, 3];
foreach ($items as &$value) {
$value *= 2;
}
unset($value); // break the dangling reference — do this every time
print_r($items);
?>Array
(
[0] => 2
[1] => 4
[2] => 6
)Conclusion
unset() destroys variables, array elements, and object properties, removing the binding so the name no longer exists. Keep these points in mind:
- It removes the name, not necessarily the value — memory is freed by PHP's reference counting and garbage collector.
- Unsetting an array element leaves a gap; use
array_values()to re-index. - It is not the same as assigning
null;unset()makes the variable cease to exist. - Inside functions it respects scope, and unsetting a by-reference parameter only breaks the local binding.
- Always
unset()the loop variable after aforeach ... as &$valueloop.
To learn more about working with the data unset() operates on, explore PHP Variables and PHP Arrays.