Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Language Constructs and Statements

PHP unset() language construct

Reference for PHP unset(): remove variables, array elements and properties, what happens to references and static variables, and the (unset) cast.

unset() removes a variable, an array element or an object property. After it runs, the name no longer exists: isset() is false and reading it raises an "undefined" warning.

It is most often used to delete a key from an array. Removing a key does not renumber the remaining keys, which surprises people who expect a list to close the gap.

PHP
unset($temp);
unset($user['password']);
unset($a, $b, $c);

Try it

PHP Removing array elements

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP Variables, references and properties

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Syntax

PHP
unset(mixed $var, mixed ...$vars): void
Name Description
$var A variable, array element ($a[0]), property ($o->p) (static properties cannot be unset).
...$vars More targets, removed from left to right.

Return value: none (void). Unsetting something that does not exist is silently ignored.

Errors: you cannot unset($this), and unset cannot be applied to a function call result. Unsetting a declared typed property makes it uninitialized, so reading it throws an Error (unless __get() handles it).

Usage notes

  • Only the name goes away. An object is destroyed (and __destruct() runs) only when the last reference to it disappears. Unsetting one of two variables pointing at the same object leaves it alive.
  • References. unset($b) where $b is a reference only breaks the link; $a keeps its value, as in the second example. This is the standard way to end a foreach (... as &$v) reference after the loop.
  • Keys are not renumbered. After unset($list[1]) the keys are 0, 2, 3. Use array_values() or array_splice() when you need a packed list.
  • Safe inside foreach. Removing the current element in a by-value loop is fine, because the loop works on a copy.
  • Inside a function, unset on a global variable only removes the local alias; use unset($GLOBALS['name']) to delete the real one. On a static variable it removes the binding for that call only.
  • Arrays are cheap to clear. $arr = [] is clearer than unsetting each key, and releases the elements just the same.
  • Memory. unset($bigArray) frees memory when nothing else references the data; see Memory and garbage collection.
  • The (unset) cast was deprecated in 7.2 and removed in PHP 8.0. Assign null instead.
  • Magic properties. Unsetting an inaccessible property calls __unset() if the class defines it.