In PHP, the statement for releasing variables is called unset (since PHP4, unset is no longer a function, but a statement). A problem occurred when unset was used a few days ago, the unset problem is summarized as follows. If you have carefully read the PHP manual, there is no need to read it again. This article is intended for students who have no intention of reading the manual.
The first thing to emphasize is that the PHP unset global variable is no longer a function in PHP. Since it is not a function, no return value is returned, therefore, the unset return value cannot be used for determination.
Second, in the function, PHP unset global variables can only destroy local variables, and cannot destroy global variables. Let's look at an example in the following manual.
- < ?PHP
- function destroy_foo() {
- global $foo;
- unset($foo);
- }
- $foo = ‘bar’;
- destroy_foo();
- echo $foo;
- ?>
The returned result is
Bar
Why? The reason is that PHP unset global variables can only destroy local variables in the function. What should I do if I need to destroy global variables in the program? It is also very simple. It is implemented using the $ GLOBALS array. See the following example:
- < ?PHP
- function foo() {
- unset($GLOBALS['bar']);
- }
- $bar = “something”;
- foo();
- var_dump($bar);
- ?>