The unset function is a function of PHP's own destruction variables, we introduced the use of unset to destroy the static variables and global variables, and can destroy the array variable Oh, let's look at the example below.
Example 1. Unset () example
<?php//destroys a single variable unset ($foo);//destroys a single array element unset ($bar [' Quux ']);//destroys more than one variable unset ($foo 1, $foo 2, $foo 3);? >
The behavior of unset () in a function depends on the type of variable you want to destroy.
If you unset () a global variable in a function, only the local variable is destroyed, and the variable in the calling environment keeps the same value as before the call to Unset ().
<?phpfunction Destroy_foo () {global $foo; unset ($foo);} $foo = ' Bar ';d estroy_foo (); Echo $foo;? >
The above example will output:
Bar
If you unset () a variable passed by reference in a function, only the local variable is destroyed, and the variable in the calling environment keeps the same value as before the call to Unset ().
<?phpfunction foo (& $bar) {unset ($bar); $bar = "blah";} $bar = ' something '; echo "$barn"; foo ($bar); echo "$barn";? >
The above example will output:
Something
Something
unset () static variable
Strictly speaking, destroying a static variable with unset () only breaks the reference between the variable name and the value of the variable.
Example:
<?phpfunction foo () { static $b; $a + +; $b + +; echo "$a---$bn"; unset ($a, $b); Var_dump ($a); Var_dump ($b); echo "##################### #n";} Foo (); foo (); foo ();? >
To run the example, output:
1---1nullnull###################### #1---2nullnull###################### #1---3nullnull#######################
unset () Global variables
As with unset () static variables, if you unset () a global variable in a function, only the local variable is destroyed, and the variable in the calling environment keeps the same value as before the call to Unset ().
Try to compare the following two examples:
<?phpfunction Destroy_foo () { global $foo; Unset ($foo);} $foo = ' Bar ';d estroy_foo (); Echo $foo;? >
<?phpfunction Destroy_foo () { global $foo; unset ($GLOBALS [' foo ']);} $foo = ' Bar ';d estroy_foo (); Echo $foo;? >
Running the first example will output: bar, and the second example will not have any output.