The unset Construct

The unset construct deletes a variable or a specified array element. If the variable no longer exists after unset, the isset function will return false. An attempt to access a deleted variable will generate a notice.

Syntax

phpunset($var);
unset($var1, $var2, $var3);
unset($array['key']);

Example

Deleting a simple variable:

php<?php
  $var = 'test';
  unset($var);
  var_dump(isset($var));
?>

Code execution result:

phpfalse

Example

Deleting an array element:

php<?php
  $arr = ['a', 'b', 'c'];
  unset($arr[1]);
  print_r($arr);
?>

Code execution result:

phpArray
(
  [0] => a
  [2] => c
)

Example

Deleting multiple variables:

php<?php
  $a = 1;
  $b = 2;
  $c = 3;
  unset($a, $b, $c);
  var_dump(isset($a), isset($b), isset($c));
?>

Code execution result:

phpfalse
false
false

See Also

  • the isset function,
    which checks for the existence of a variable
  • the empty function,
    which checks if a variable is empty