将 NULL 分配给 PHP 中的变量:有什么作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11158772/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:52:47  来源:igfitidea点击:

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

phpnull

提问by toon81

In maintaining code, I'm encountering loops, where at the end of the loop several variables are set to NULLlike so: $var = NULL;. From what I understand in the manual, NULL is meant mostly as something to compare against in PHP code. Since NULLhas no type and is not a string or number, outputting it makes no sense.

在维护代码时,我遇到了循环,在循环结束时,几个变量被设置为NULL这样:$var = NULL;. 根据我在手册中的理解,NULL 主要用于在 PHP 代码中进行比较。由于NULL没有类型并且不是字符串或数字,因此输出它没有意义。

I unfortunately cannot provide an example, but I think the NULL values are being written to a file in our code. My question is: does $varhave a value after the assignment, and will echoing/writing it produce output?

不幸的是,我无法提供示例,但我认为 NULL 值正在我们的代码中写入文件。我的问题是:$var赋值后是否有值,并且回显/写入它会产生输出吗?

EDIT:I have read the PHP manual entry onNULL. There is no need to post this: http://php.net/manual/en/language.types.null.phpin a comment or answer, or top downvote me for not having RTM. Thank you!

编辑:我已经阅读了 .php 的 PHP 手册条目NULL。没有必要在评论或答案中发布这个:http: //php.net/manual/en/language.types.null.php,或者因为没有 RTM 而对我投反对票。谢谢!

回答by ghoti

[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. The isset()function would be better named isnotnull(). :-P

的结果isset()将是布尔假,但变量仍被定义。该isset()函数最好命名为isnotnull()。:-P

Note that is_null()will also return true for a value that has never been set.

请注意,is_null()对于从未设置过的值,它也将返回 true。

Yay PHP.

耶 PHP。

回答by ThiefMaster

nullis pretty much just like any other value in PHP (actually, it's also a different data type than string, int, etc.).

null几乎就像 PHP 中的任何其他值一样(实际上,它也是与 string、int 等不同的数据类型)。

However, there is one important difference: isset($var)checks for the var to exist and have a non-null value.

但是,有一个重要的区别:isset($var)检查 var 是否存在并具有非空值。

If you plan to read the variable ever again before assigning a new value, unset()is the wrong way to do but assigning nullis 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 nullsets it to a specific value (and creates the variable if necessary).

如您所见,unset()实际上删除了变量,就像它从未存在过一样,而assignnull将其设置为特定值(并在必要时创建变量)。

A useful use-case of nullis 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) { ... }
}

回答by Salman A

A variable could be set to NULLto indicate that it does not contain a value. It makes sense if at some later point in the code the variable is checked for being NULL.

可以将变量设置为NULL指示它不包含值。如果在代码稍后的某个时刻检查变量是否为 ,这是有道理的NULL

A variable might be explicitly set to NULL to release memory used by it. This makes sense if the variable consumes lots of memory (see this question).

一个变量可能被显式设置为 NULL 以释放它使用的内存。如果变量消耗大量内存,这是有道理的(请参阅此问题)。

Dry run the code and you might be able to figure out the exact reason.

试运行代码,您可能会找出确切原因。

回答by Arno 2501

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

回答by RobB

It appears that the purpose of the null implementation based off of the information provide is to clear the variable.

似乎基于提供的信息的 null 实现的目的是清除变量。

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.

The behavior of unset() inside of a function can vary depending on what type of variable you are attempting to destroy.

If a globalized variable is unset() inside of a function, only the local variable is destroyed. The variable in the calling environment will retain the same value as before unset() was called.

unset() 销毁指定的变量。

函数内 unset() 的行为可能因您试图销毁的变量类型而异。

如果函数内部的一个全局变量是 unset(),则只有局部变量会被销毁。调用环境中的变量将保留与调用 unset() 之前相同的值。

回答by Rajkumar sudhirkumar

Null is a special data type which can have only one value, which is itself. 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 的变量是一个没有赋值的变量,当创建没有值的变量时,它会自动赋值为 null,这是以便清除之前该内存位置中的任何垃圾,否则程序可能会尝试处理它