PHP 重置变量的几种方法

问题描述 投票:0回答:1

我知道一些在 PHP 中重置变量的方法。

问题是我不知道到底有什么区别 他们谁更快,所以我在这里问......

有什么区别:

<?php

$resetME = null;
//VS    
unset($resetME);
//VS
$resetME = 0;

?>

还有...

我很高兴知道是否还有其他技巧可以从内存中完全删除变量。

此外,我很想知道是否有任何理由在页面末尾重置变量,因为当页面完全加载时服务器无论如何都会重置它们......

只是为了澄清更多...

<?php

$a = 1;
echo $a;

//is there any reason to unset $a on the end of the page ?
unset($a);

?>

提前致谢!

php string variables null unset
1个回答
3
投票
$resetME = null; // Will not erase var from the memory
  
unset($resetME); // Will erase var from the memory

$resetME = 0; // Will not erase var from the memory

要从内存中完全删除变量,您需要使用 unset。 所有其他方法只是改变变量值。但如果变量不存在,您将通过使用

unset($resetME);
收到错误,在这种情况下您可以使用
$resetME = null;
if (isset($resetME)) {unset($resetME)}

无论如何,脚本结束后,PHP 会清理内存。

© www.soinside.com 2019 - 2024. All rights reserved.