unset
Destroys a variable or an array element.
Syntax
unset(mixed $var, mixed ...$vars): voidParameters
| Parameter | Type | Description |
|---|---|---|
| $var | mixed | A variable, an array element or a property. |
| ...$vars | mixed | Other variables. |
Return value
voidThe construct returns nothing.
Examples
Removing an element by key:
<?php
$user = ['name' => 'Anna', 'password' => 'secret'];
unset($user['password']);
echo json_encode($user);Output:
{"name":"Anna"}A list is left with a gap after a removal:
<?php
$numbers = [10, 20, 30];
unset($numbers[1]);
echo json_encode($numbers), "\n";
echo json_encode(array_values($numbers));Output:
{"0":10,"2":30}
[10,30]Notes
The keys of a list are not renumbered. If you need a list without gaps, call array_values() or remove with array_splice().
See also
- isset — Checks that a variable exists and is not null.
- array_splice — Removes part of an array and optionally inserts new elements in its place.
In the tutorial
- Variables — How to declare variables in PHP, name them and interpolate them into strings.