简体   繁体   English

在PHP中为变量分配NULL:这是做什么的?

[英]Assigning NULL to a variable in PHP: what does that do?

In maintaining code, I'm encountering loops, where at the end of the loop several variables are set to NULL like so: $var = NULL; 在维护代码时,我遇到了循环,循环结束时,几个变量被设置为NULL如下所示: $var = NULL; . From what I understand in the manual, NULL is meant mostly as something to compare against in PHP code. 据我在手册中所了解的,NULL主要是要与PHP代码进行比较的意思。 Since NULL has no type and is not a string or number, outputting it makes no sense. 由于NULL没有类型,也不是字符串或数字,因此输出NULL是没有意义的。

I unfortunately cannot provide an example, but I think the NULL values are being written to a file in our code. 不幸的是,我无法提供示例,但是我认为NULL值正在被写入我们代码中的文件中。 My question is: does $var have a value after the assignment, and will echoing/writing it produce output? 我的问题是: $var在赋值后是否有值,并且回显/写入它会产生输出吗?

EDIT: I have read the PHP manual entry on NULL . 编辑: 我已经阅读了 NULL 上的PHP手册 There is no need to post this: http://php.net/manual/en/language.types.null.php in a comment or answer, or top downvote me for not having RTM. 无需在评论或答案中发布此内容: http ://php.net/manual/zh-CN/language.types.null.php,也可以因没有RTM而对我不满意。 Thank you! 谢谢!

[ghoti@pc ~]$ php -r '$i="foo"; print "ONE\n"; var_dump($i); unset($i); print "TWO\n"; var_dump($i); $i=NULL; print "THREE\n"; var_dump($i); print "\n"; if (isset($i)) print "Set.\n"; if (is_null($i)) print "is_null\n";'
ONE
string(3) "foo"
TWO
NULL
THREE
NULL
is_null
[ghoti@pc ~]$ 

The result of isset() will be boolean false, but the variable is still defined. isset()的结果将为布尔值false,但仍定义了该变量。 The isset() function would be better named isnotnull() . isset()函数最好命名为isnotnull() :-P :-P

Note that is_null() will also return true for a value that has never been set. 请注意,对于从未设置的值, is_null()也将返回true。

Yay PHP. 是的PHP。

null is pretty much just like any other value in PHP (actually, it's also a different data type than string, int, etc.). null与PHP中的任何其他值几乎一样(实际上,它也是与字符串,int等不同的数据类型)。

However, there is one important difference: isset($var) checks for the var to exist and have a non-null value. 但是,有一个重要的区别: isset($var)检查var是否存在并具有非null值。

If you plan to read the variable ever again before assigning a new value, unset() is the wrong way to do but assigning null is perfectly fine: 如果您打算在分配新值之前再次读取变量,则unset()是错误的方法,但是分配null则可以:

php > $a = null;
php > if($a) echo 'x';
php > unset($a);
php > if($a) echo 'x';
Notice: Undefined variable: a in php shell code on line 1
php >

As you can see, unset() actually deletes the variable, just like it never existed, while assigning null sets it to a specific value (and creates the variable if necessary). 如您所见, unset()实际上删除了变量,就像它从未存在一样,而分配null会将其设置为特定值(并在必要时创建变量)。

A useful use-case of null is in default arguments when you want to know if it was provided or not and empty strings, zero, etc. are valid, too: 当您想知道是否提供了null ,在有用的用例情况下使用默认参数,并且空字符串,零等也有效:

function foo($bar = null) {
    if($bar === null) { ... }
}

A variable could be set to NULL to indicate that it does not contain a value. 可以将变量设置为NULL以指示它不包含值。 It makes sense if at some later point in the code the variable is checked for being NULL . 如果在代码的稍后部分检查变量是否为NULL ,则是有意义的。

A variable might be explicitly set to NULL to release memory used by it. 变量可以显式设置为NULL,以释放它使用的内存。 This makes sense if the variable consumes lots of memory (see this question ). 如果变量消耗大量内存,这是有道理的(请参阅此问题 )。

Dry run the code and you might be able to figure out the exact reason. 空运行代码,您也许可以找出确切的原因。

Null in PHP means a variable were no value was assigned. PHP中的Null表示未分配任何变量。

http://php.net/manual/en/language.types.null.php http://php.net/manual/en/language.types.null.php

It appears that the purpose of the null implementation based off of the information provide is to clear the variable. 似乎基于信息提供的空实现的目的是清除变量。

You can unset a variable in PHP by setting it to NULL or using the function unset() . 您可以通过将其设置为NULL或使用函数unset()来取消设置PHP中的变量。

unset() destroys the specified variables. unset()销毁指定的变量。

The behavior of unset() inside of a function can vary depending on what type of variable you are attempting to destroy. 函数内部的unset()行为可能会有所不同,具体取决于您要破坏的变量类型。

If a globalized variable is unset() inside of a function, only the local variable is destroyed. 如果全局变量在函数内部为unset(),则仅销毁局部变量。 The variable in the calling environment will retain the same value as before unset() was called. 调用环境中的变量将保留与调用unset()之前相同的值。

Null is a special data type which can have only one value, which is itself. Null是一种特殊的数据类型,它本身只能有一个值。 Which is to say null is not only a data type but also a keyword literal a variable of data type null is a variable that has no value assigned to it when a variable is created without a value it is automatically assigned a value of null this is so that whatever garbage was in that memory location before is cleared out otherwise the program may try to process it 也就是说,null不仅是数据类型,而且还是关键字文字,数据类型为null的变量是在创建变量时没有赋值的变量,而没有值时,它会自动分配为null,这是这样就可以清除之前该内存位置中的所有垃圾,否则程序可能会尝试对其进行处理

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM