The function unset() destroys the specified variables. The behavior of unset() inside of a function can vary depending on what type of variable you are attempting to destroy.
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.
void unset ( mixed $var , mixed $vars )
Sr.No | Parameter & Description |
---|---|
1 | var Mandatory.he variable to be unset. |
2 |
vars Optional.Further variables. |
This function does not return any value.
PHP 4 and above
Following example demonstrates use of unset() function −
<?php $a = "Welcome TutorialsPoint!"; echo "The value of 'a' before unset: " . $a . "<br>"; unset($a); echo "The value of 'a' after unset: " . $a; ?>
This will produce following result −
The value of 'a' before unset: Welcome TutorialsPoint! The value of 'a' after unset:
Following example demonstrates case when globalized variable is unset() inside of a function −
<?php //globalized variable is unset() inside of a function function destroy_a() { global $a; unset($a); } $a = 'Welcome TutorialsPoint!'; destroy_a(); echo "global unset inside function =".$a; ?>
This will produce following result −
global unset inside function =Welcome TutorialsPoint!
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. Following example demonstrates this −
<?php function destroy_c($v) { unset($c); $c = "heloo"; } $c = 'TutorialsPoint'; echo "$c<br>"; destroy_c($c); echo "$c<br>"; ?>
This will not produce result −
TutorialsPoint TutorialsPoint