gpt4 book ai didi

php - unset() 静态变量不起作用?

转载 作者:行者123 更新时间:2023-12-03 02:42:49 26 4
gpt4 key购买 nike

看这段代码: http://codepad.org/s8XnQJPN

function getvalues($delete = false)
{
static $x;
if($delete)
{
echo "array before deleting:\n";
print_r($x);
unset($x);
}
else
{
for($a=0;$a<3;$a++)
{
$x[]=mt_rand(0,133);
}
}
}

getvalues();
getvalues(true); //delete array values
getvalues(true); //this should not output array since it is deleted

输出:

array before deleting:
Array
(
[0] => 79
[1] => 49
[2] => 133
)
array before deleting:
Array
(
[0] => 79
[1] => 49
[2] => 133
)

为什么数组$x在取消设置时没有被删除?

最佳答案

如果取消设置静态变量,则仅在取消设置的函数中销毁该变量。以下对函数 (getValues()) 的调用将使用取消设置之前的值。

unset 函数的文档中也提到了这一点。 http://php.net/manual/en/function.unset.php

关于php - unset() 静态变量不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9443753/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com