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.
unset($temp); unset($user['password']); unset($a, $b, $c);
Try it
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.
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
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$bis a reference only breaks the link;$akeeps its value, as in the second example. This is the standard way to end aforeach (... as &$v)reference after the loop. - Keys are not renumbered. After
unset($list[1])the keys are0, 2, 3. Usearray_values()orarray_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,
unseton aglobalvariable only removes the local alias; useunset($GLOBALS['name'])to delete the real one. On astaticvariable 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. Assignnullinstead. - Magic properties. Unsetting an inaccessible property calls
__unset()if the class defines it.
Related
isset,empty,array_splice(),array_values(), References- Lessons: Arrays, Variable scope