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
The code is as follows |
Copy Code |
Destroying a single variable Unset ($foo); Destroying a single array element unset ($bar [' Quux ']); Destroy 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 ().
The code is as follows |
Copy Code |
function Destroy_foo () { Global $foo; Unset ($foo); } $foo = ' Bar '; Destroy_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 ().
The code is as follows |
Copy Code |
function 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:
The code is as follows |
Copy Code |
function 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---1 Null Null ####################### 1---2 Null Null ####################### 1---3 Null Null ####################### |
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:
The code is as follows |
Copy Code |
function Destroy_foo () { Global $foo; Unset ($foo); } $foo = ' Bar '; Destroy_foo (); Echo $foo; ?> function Destroy_foo () { Global $foo; unset ($GLOBALS [' foo ']); } $foo = ' Bar '; Destroy_foo (); Echo $foo; ?> |
Running the first example will output: bar, and the second example will not have any output.
http://www.bkjia.com/PHPjc/628977.html www.bkjia.com true http://www.bkjia.com/PHPjc/628977.html techarticle The unset function is a function of PHP's own destruction variables, we introduced the use of unset to destroy static variables and global variables, but also out can destroy the array variable Oh, let's see ...