18 of 410 menu

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

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

Example

Deleting a simple variable:

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

Code execution result:

false

Example

Deleting an array element:

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

Code execution result:

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

Example

Deleting multiple variables:

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

Code execution result:

false 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
byenru