The unset function is a function that comes with PHP to destroy variables. We introduced the method of using unset to destroy static variables and global variables. At the same time, we also introduced the method of destroying array variables. Let's see an example below.
Example 1. unset() example
The code is as follows
Copy code
代码如下
复制代码
// 销毁单个变量
unset ($foo);
// 销毁单个数组元素
unset ($bar['quux']);
// 销毁一个以上的变量
unset ($foo1, $foo2, $foo3);
?>
// Destroy a single variable
unset ($foo);
//Destroy a single array element
代码如下
复制代码
function destroy_foo() {
global $foo;
unset($foo);
}
$foo = ‘bar’;
destroy_foo();
echo $foo;
?>
unset ($bar['quux']);
// Destroy more than one variable
unset ($foo1, $foo2, $foo3);
?>
The behavior of unset() in a function will vary depending on the type of variable you want to destroy.
代码如下
复制代码
function foo(&$bar) {
unset($bar);
$bar = “blah”;
}
$bar = ‘something’;
echo “$barn”;
foo($bar);
echo “$barn”;
?>
上边的例子将输出:
something
something
If you unset() a global variable in a function, only the local variable will be destroyed, and the variables in the calling environment will maintain the same value before calling unset().
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 will be destroyed, and the variables in the calling environment will retain the same value before calling 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, using unset() to destroy a static variable only breaks the reference between the variable name and the variable value.
Example:
unset() global variable
Like unset() static variables, if you unset() a global variable in a function, only the local variable will be destroyed, and the variables in the calling environment will retain the same value before calling unset().
Try to compare the following two examples:
The code is as follows
代码如下
复制代码
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;
?>
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 , while the second example will not output anything.
http://www.bkjia.com/PHPjc/628977.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/628977.htmlTechArticleThe unset function is a function that comes with PHP to destroy variables. We introduced the use of unset to destroy static variables and globals. The variable method can also destroy array variables. Let’s see below...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn