unset

Destroys one or more variables, array elements or object properties in the current scope.

Signature
unset(mixed $var, mixed ...$vars): void
  • Added in PHP 4.0

Parameters

ParameterTypeDescription
$varmixedThe variable to be unset.
$varsoptionalmixedFurther variables to unset in the same call.

Return value

voidNo value is returned.

Errors

unset() is a language construct, not a function: it cannot be used as a callback or with named arguments, and $this cannot be unset inside an object method.

unset() removes a variable from the current scope, deletes an array element, or removes an object property. It returns nothing. After unset($x), isset($x) is false and reading $x raises an undefined-variable warning.

When to reach for it

The two everyday uses are dropping a key from an array — stripping a password field from a row before returning it — and releasing a large value early inside a long-running loop so the garbage collector can reclaim it. It takes multiple arguments, so unset($a, $b, $c) clears several at once.

What trips people up

Unsetting an element does not reindex the array. Remove index 1 from a three-element list and you are left with keys 0 and 2 — which json_encode() then serialises as an object {"0":...,"2":...} instead of an array. Run the result through array_values(), or use array_splice(), which reindexes for you.

Scope matters more than it looks. Unsetting a parameter that was passed by reference only destroys the local alias; the caller's variable is untouched. The same is true of a variable imported with global — to remove the real global, unset $GLOBALS['name'].

unset() also does not free memory immediately if another variable still references the same value; it only drops one reference.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Stripping a password or token column from a row before returning it as JSON
  • Freeing a large decoded payload inside a long-running import loop
  • Removing a temporary key added during a data transformation

Frequently asked questions

Does unset reindex an array?

No. The remaining keys keep their original values, leaving gaps in a numeric sequence. Use array_values() to reindex or array_splice() to remove and renumber in one step.

Does unset free memory immediately?

Only if nothing else references the value. unset() drops one reference; memory is reclaimed when the refcount reaches zero, which is immediate for a sole reference but delayed when copies or references remain.

Why does unset inside a function not affect my variable?

unset() only works in the current scope. For a by-reference parameter it destroys the local alias, and for a global-imported variable it destroys the local copy — use unset($GLOBALS['name']) to remove a real global.

More examples

Stripping sensitive fields from a row

unset() with multiple arguments removes both secrets in one statement, and string keys are unaffected by the reindexing problem. array_diff_key() shows the non-mutating alternative when you need to keep the original.

Example · php
<?php
$user = [
    'id' => 7, 'email' => '[email protected]',
    'password_hash' => '$2y$10$abc', 'api_token' => 'sk_live_123',
];

unset($user['password_hash'], $user['api_token']);
echo json_encode($user), "\n";

$safe = array_diff_key($user, array_flip(['email']));
echo json_encode($safe), "\n";

Scope: references and globals

Unsetting a by-reference parameter only breaks the local alias, so $bar survives the first call. Removing the entry from $GLOBALS is what actually destroys the global variable.

Example · php
<?php
$bar = 'still here';

function dropLocal(&$ref) { unset($ref); }
function dropGlobal() { unset($GLOBALS['bar']); }

dropLocal($bar);
var_dump(isset($bar));

dropGlobal();
var_dump(isset($bar));

Discussion

  • Be the first to comment on this lesson.