PHP Function Reference

PHP unset() Function



The PHP unset() function is not really a function, but a language construct and used to unset the specified variables. The behavior of unset() inside of a function depends upon the type of variable which is attempted to be destroyed.

  • If a globalized variable is unset() inside of a function, only the local variable is destroyed. The variable in the calling environment will retain the same value as before unset() was called.
  • To unset() a global variable inside of a function, $GLOBALS array can be used.
  • If a variable that is passed by reference is unset() inside of a function, only the local variable is destroyed. The variable in the calling environment will retain the same value as before unset() was called.
  • If a static variable is unset() inside of a function, unset() destroys the variable only in the context of the rest of a function. Following calls will restore the previous value of a variable.
Note: It is possible to unset object properties visible in current context. It is not possible to unset $this inside an object method.

Syntax

unset(var, vars)

Parameters

var Required. Specify the variable to be unset.
vars Optional. Specify the further variables to be unset. Multiple parameters are allowed.

Return Value

No value is returned.

Example: unset() example

The example below shows the usage of unset() function.

<?php
//destroy a single variable
unset($test);

//destroy multiple variables
unset($test1, $test2, $test3);

//destroy a single element of an array
unset($arr['xyz']);
?>

Example: using unset() with global variable inside of a function

When a global variable is unset() inside of a function, only the local variable is destroyed. The variable in the calling environment will retain the same value as before unset() was called. Consider the example below:

<?php
$var = 'Hello';

function destroy_var() {
  global $var;
  unset($var);
}

destroy_var();
echo "Value of \$var: ".$var;
?>

The output of the above code will be:

Value of $var: Hello

Example: unset() a global variable inside of a function

To unset() a global variable inside of a function, $GLOBALS array can be used. See the example below:

<?php
$var = 'Hello';

function destroy_var() {
  global $var;
  unset($GLOBALS['var']);
}

destroy_var();
echo "Value of \$var: ".$var;
?>

The output of the above code will be:

Value of $var: 
PHP Warning:  Undefined variable $var in Main.php on line 10

Example: unset() a passed by reference variable

If a variable that is passed by reference is unset() inside of a function, only the local variable is destroyed. The variable in the calling environment will retain the same value as before unset() was called. Consider the example below:

<?php
function foo(&$var) {
  unset($var);
  $var = "World";
}

$var = 'Hello';
echo "$var\n";

foo($var);
echo "$var\n";
?>

The output of the above code will be:

Hello
Hello

Example: unset() a static variable inside of a function

If a static variable is unset() inside of a function, unset() destroys the variable only in the context of the rest of a function. Following calls will restore the previous value of a variable. See the example below:

<?php
function foo() {
  static $var;
  $var++;
  echo "Before unset: $var, ";
  unset($var);
  $var = 50;
  echo "After unset: $var\n";
}

foo();
foo();
foo();
?>

The output of the above code will be:

Before unset: 1, After unset: 50
Before unset: 2, After unset: 50
Before unset: 3, After unset: 50

❮ PHP Variable Handling Reference