The unset() function is used to destroy the given variable.
PHP version requirements: PHP 4, PHP 5, PHP 7
void unset ( mixed $var [, mixed $... ] )
Parameter description:
$var: variable to be destroyed.
No return value.
If you unset() a global variable in a function, only the local variable is destroyed, and the variables in the calling environment will retain the same value before calling unset().
The output is:
bar
If you want to unset() a global variable in a function, you can use the $GLOBALS array:
If you unset() a variable passed by reference in a function, only the local variable is destroyed, and the variables in the calling environment will retain the same value before calling unset().
The above routine will output:
somethingsomething
If you unset() a static variable in a function, the static variable will be destroyed inside the function. However, when this function is called again, this static variable will be restored to the value it had before it was last destroyed.
The above routine will output:
Before unset: 1, after unset: 23Before unset: 2, after unset: 23Before unset: 3, after unset: 23