PHP unset() Function
unset() The function is used to destroy the given variable.
PHP version requirement: PHP 4, PHP 5, PHP 7
Syntax
void unset ( mixed $var [, mixed $... ] )
Parameter Description:
- $var: the variable to destroy.
Return Value
No value is returned.
Example
Example
If you unset() a global variable inside a function, only the local variable is destroyed, while the variable in the calling environment retains the value it had before the call to unset().
Example
The output is:
bar
If you want to unset() a global variable inside a function, you can use the $GLOBALS array:
Example
If you unset() a variable passed by reference inside a function, only the local variable is destroyed, while the variable in the calling environment retains the value it had before the call to unset().
Example
The above example will output:
something
something
If you unset() a static variable inside a function, that static variable will be destroyed inside the function. However, when the function is called again, this static variable will be restored to the value it had before being destroyed.
Example
The above example will output:
Before unset: 1, after unset: 23
Before unset: 2, after unset: 23
Before unset: 3, after unset: 23
Available PHP Functions