PHP unset() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Variables

Definition

In PHP, unset() destroys one or more variables. After you call it, the variable no longer exists, which means isset() returns false for it. It is a language construct, not a normal function.

You can also use unset() to remove an item from an array by its key. This is the usual way to delete array elements. Inside a function, unset() only removes the local name of a variable, so a global variable with the same name is not affected.

Syntax

Call unset() with the values you need. Start with this line.

PHP
unset($name);
CallWhat it does
unset($name)Removes the variable $name completely.
unset($list[1])Removes the element with key 1 from the array $list.
unset($a, $b)Removes both variables in a single call.

unset() on an array element does not reindex the array. Use array_values() afterward if you need keys that start at 0 again. To keep the variable but clear its value, assign null instead.

Library declarations
PHP
unset(mixed $var, mixed ...$vars): void

Parameters

ParameterDescription
varRequired. The variable, array element or property to remove.
varsOptional. Any number of extra variables to remove in the same call.

Return Value

Return ValueDescription
voidunset() does not return anything. Do not use it inside an expression or assign its result.

PHP Examples

Example 1 — Removing a Variable

Create a variable, check it with isset(), remove it with unset() and check it again.

PHP
<?php
$name = 'Asha';
echo isset($name) ? 'set' : 'not set', PHP_EOL;

unset($name);
echo isset($name) ? 'set' : 'not set', PHP_EOL;

How It Works

  1. $name is created with a value. isset() finds it and the first line prints set.
  2. unset($name) destroys the variable. It no longer exists after this line.
  3. isset() now returns false. The second line prints not set.
  4. No warning is shown. isset() is safe to use on a variable that does not exist.

Example 2 — Array Keys After unset()

Remove one item from an array and look at the keys. Then renumber them with array_values().

PHP
<?php
$colors = ['red', 'green', 'blue'];

unset($colors[1]);
print_r($colors);

$colors = array_values($colors);
print_r($colors);

How It Works

  1. The array starts with keys 0, 1 and 2. green has the key 1.
  2. unset($colors[1]) removes green. The other items keep their old keys, so key 1 is missing.
  3. print_r() shows the gap. The keys are now 0 and 2.
  4. array_values() renumbers the keys. The final array has the keys 0 and 1 again.

Example 3 — References and Function Scope

See what unset() does not do: it does not delete the original value behind a reference, or a global variable when used inside a function.

PHP
<?php
$original = 'data';
$ref = &$original;
unset($ref);
echo $original, PHP_EOL;

function test() {
    global $count;
    unset($count);
    echo isset($count) ? 'local set' : 'local gone', PHP_EOL;
}

$count = 5;
test();
echo $count, PHP_EOL;

How It Works

  1. $ref is a reference to $original. Both names point to the same value.
  2. unset($ref) removes only that name. $original still holds data.
  3. Inside test(), global $count creates a local link. unset($count) removes that link, so isset() reports it gone.
  4. The global $count is untouched. After the function ends, echo $count still prints 5.

Frequently Asked Questions

It destroys a variable or removes an array element. After unset($x), the variable no longer exists and isset($x) returns false.
No. It is a language construct, like isset() and echo. It does not return a value and cannot be used as a callback name.
No. The remaining items keep their original keys, which leaves a gap. Call array_values() afterward if you want the keys renumbered from 0.
unset($x) removes the variable, so isset($x) is false and using it raises a warning. $x = null keeps the variable but gives it the value null. isset($x) is false for both.
Not by itself. unset($x) inside a function only removes the local name. To remove the real global, use unset($GLOBALS['x']).

Next: var_dump()

var_dump() prints detailed information about one or more values, including their types and.

var_dump() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful