English 中文(简体)
设定变量与设定
原标题:Unsetting a variable vs setting to

采取下列行动是否更好? 如果不是的话,其中之一是否比另一方更快?

unset($variable);

$variable =   ;
最佳回答

他们的工作将略有不同:

  • 如果不确定,将从文号表中删除变量,并将在内容上将参考数减到1,在这样之后,提及变量将引发通知(“未界定变量”)。 (注,一物体可通过实施__unset()而超越对其财产的默认行为)。

  • 空洞将减少内容的参考量1,将内容的字面限定在0容的方位上,但文号仍保留在文号表中,你仍然可以参考变量。 (注,一物体可通过实施__set()而超越对其财产的默认转让行为)。

在旧的营地里,当重新计算时,即叫 des子,并立即释放记忆。 在新的版本(>=5.3)中, php采用一种缓冲办法,更好地处理周期性参考资料(),因此,记忆有可能在以后释放,根本不可能拖延......无论如何,这确实造成任何问题,并防止某些新的泄漏。

如果能够再次使用变数的名称,那么,不定就应当更快地成为几个pu周期(因为不需要创造新的内容)。 但是,如果重新使用变数名称,那么,php就必须创造新的变数和符号表条目,这样会放慢速度! 在大多数情况下,iff夫的差别微不足道。

如果你想把变数贴上以后的检查无效的标记,你可以将其定为虚假或无效。 这比对 is( is)的测试要好,因为改名的打字将不作任何错误,否则就会退回不实......你也可以把虚假的、无效的价值观传递给另一个功能,保留所投的价值,而这种价值只能用不定的var......

也就是说:

$var = false; ...
if ($var !== false) ...

$var = null; ...
if (!is_null($var)) ...

would be better f或 checking sentinel values than

unset($var); ...
if (isset($var)) ...
问题回答

缩略语

if(isset($test))

由于它仍然固定下来,它只是空洞。

然而,它真的会恢复。

if(empty($test))

它是一个空洞变量。 这取决于你正在检查什么。 一般来说,人们倾向于检查变数是否正在形成,而不是如果是空的。

因此,最好彻底放弃。

此外,更容易理解这一点。

unset($test);

页: 1

$test =   ;

第一种情况立即告诉你,变量是LONGER SET。 如果后者简单地告诉你,它就被安排到空白处。 通常,如果你要把 st子添加到一个变数中,而不想把购买力平价错误带上你。

你正在做不同的事情,<代码>unset<><>>>t<>/code>的目的是在你作出这一规定时销毁特定的变数,你的第二例只是将变数变成空洞。

如果你对业绩感到关切,将变数改为<>NUL 这可能是一个更好的选择,但实际而言,差异不会明显......

docs :

unset() does just what it s name says - unset a variable. It does not force immediate memory freeing. PHP s garbage collector will do it when it see fits - by intention as soon, as those CPU cycles aren t needed anyway, or as late as before the script would run out of memory, whatever occurs first.

If you are doing $whatever = null; then you are rewriting variable s data. You might get memory freed / shrunk faster, but it may steal CPU cycles from the code that truly needs them sooner, resulting in a longer overall execution time.

我认为最相关的区别是,不设定一个变量就表明,如果你试图使用该变量,则该变量不会在随后的法典中使用(也通过报告E_NOTICE而使用),因为jspcal说,该变量不会再出现在文号表中。

因此,如果空洞是你与你变数做的(或发送的)合法价值的话,就会走下去,并把它定下来。 否则,如果变数不再有用,则使变数更清楚的编码意图无法实现。

它们具有完全不同的含义。 前者是一个可变的非重要因素。 后者只是把其价值放在空洞上。 说话是“最好”的问题,因为它们完全不同。

你们是否试图清理记忆? 如果是的话,不要拖网;PHP为你管理记忆,这样你就可以离开,自动清理。

如果你不设法清理记忆,那么你需要标出why,你想打开一个变数或把它 empty空,并选择适当的变量。 就此进行的一次良好检查:请允许我说,有人在你不问世后,在某个地方插入以下法典:

if(strcmp($variable,   ) == 0) { do_something(); }

其后:

if(!isset($variable)) { do_something_else(); }

如果你把变数定在空壳上,则第一部将使用<代码>do_something()。 如果你不确定变量,则将使用<代码>do_something_else(。 如果您的描述是恰当行为的话,你会做些什么?

这里还有一句话。

如果是:

$a =  foobar ;
$variable =& $a;

那么,你两种选择中哪一种是完全不同的。

$variable =   ;

定出可变的美元和空洞的美元。

unset($variable);

在从文号表中删除可变的美元时,删除“a”和“可变”之间的参考联系。 这的确是不把美元与美元挂钩的唯一办法,而不必将美元固定在别处。 注,例如,可变 = 无;得起。





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签