php null 和 empty 和有什么不一样?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5615747/
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
What is the difference between null and empty?
提问by PeanutsMonkey
I am new to the concept of empty and null. Whilst I have endeavoured to understand the difference between them, I am more confused. I came across an article at http://www.tutorialarena.com/blog/php-isset-vs-empty.phphowever I still don't see when you would use isset and empty when validating forms. Seeing that I don't grasp the difference, I don't want to be using the incorrect functions as well as not be able to use the functions in other areas. Can someone give examples that will help me understand? I am very new to coding so would appreciate if someone could give me real world examples and at the same time keep it simply enough for noob to follow.
我对空和空的概念很陌生。虽然我已经努力了解它们之间的区别,但我更加困惑。我在http://www.tutorialarena.com/blog/php-isset-vs-empty.php 上看到了一篇文章,但是我仍然没有看到在验证表单时何时使用 isset 和 empty 。看不懂区别,不想用错的功能,也不想用其他方面的功能。有人可以举例帮助我理解吗?我对编码很陌生,所以如果有人能给我真实世界的例子,同时保持它足够简单,让菜鸟遵循,我将不胜感激。
回答by alex
A variable is NULL
if it has no value, and points to nowhere in memory.
一个变量是NULL
如果它没有值,并且不指向内存中的任何地方。
empty()
is more a literal meaning of empty, e.g. the string ""
is empty, but is notNULL
.
empty()
更像是空的字面意思,例如字符串""
是空的,但不是NULL
。
The following things are considered to be empty:
- "" (an empty string)
- 0 (0 as an integer)
- 0.0 (0 as a float)
- "0" (0 as a string)
- NULL
- FALSE
- array() (an empty array)
- var $var; (a variable declared, but without a value in a class)
以下内容被认为是空的:
- ""(空字符串)
- 0(0 作为整数)
- 0.0(0 作为浮点数)
- “0”(0 作为字符串)
- 空值
- 错误的
- array()(空数组)
- var $var; (声明的变量,但在类中没有值)
来源。
Example
例子
$a
is NULL
.
$a
是NULL
。
$a = ''
is empty, but not NULL
.
$a = ''
是空的,但不是NULL
。
Update
更新
If
$a=''
is empty but notNULL
, when do I use theempty()
function and when do I use theisset()
function.
如果
$a=''
为空但不为空NULL
,我何时使用该empty()
函数以及何时使用该isset()
函数。
isset()
will return FALSE
is the variable is pointing to NULL
.
isset()
将返回FALSE
指向的变量NULL
。
Use empty()
when you understand what is empty(look at the list above).
使用empty()
时,你明白什么是空的(看看上面的列表)。
Also when you say it points nowhere in memory, what does that mean exactly?
此外,当您说它在内存中无处可去时,这究竟是什么意思?
It means that $str = ''
will be in memory as a string with length of 0.
这意味着$str = ''
它将作为长度为 0 的字符串存在于内存中。
If it were $str = NULL
, it would not occupy any memory.
如果是$str = NULL
,它不会占用任何内存。
回答by Ira Baxter
Nullis a placeholder that generally means "no data about this is available".
Null是一个占位符,通常意味着“没有可用的数据”。
The use of null for this is just a convention, but a rather widespread one, to the point where some programming languages support the convention directly. The reason this convention exists has IMHO historically to do with "pointers"; many times a procedure will be defined to return a pointer to an answer, and will return what is traditionally called a Null pointer if it could not produce an answer for some reason.
为此使用 null 只是一种约定,但相当普遍,以至于某些编程语言直接支持该约定。恕我直言,这个约定存在的原因在历史上与“指针”有关;很多时候,一个过程将被定义为返回一个指向答案的指针,如果由于某种原因它不能产生答案,它将返回传统上称为空指针的东西。
Emptymeans (if this is a set) that it has no members. That's an explicit answer, and it is very different than "no data about this is available".
空意味着(如果这是一个集合)它没有成员。这是一个明确的答案,它与“没有可用数据”大不相同。
In the PHP world, apparantly uninitialized variables have the Null value, and isset on such a variable returns FALSE. For arrays and strings, PHP follows the convention that "empty" means "has no members" although arrays and strings are not technically sets.
在 PHP 世界中,明显未初始化的变量具有 Null 值,并且对此类变量的 isset 返回 FALSE。对于数组和字符串,PHP 遵循“空”意味着“没有成员”的约定,尽管数组和字符串在技术上不是集合。
PHP apparantly has this funny idea that 0 and 0.0 are also "empty", by PHP design. That's abusive of the concept of "empty" IMHO: Individual numbers are not sets, so 0 can't reasonably by "empty". THis just leads to obscure programming because it violates the principle of least surprise. I'm sure the PHP designers would are that "zero is the empty number" as some kind of vague analogy; but the if analogy is vague, why bother with it? But then PHP is full of silly ideas.
PHP 显然有一个有趣的想法,即 0 和 0.0 也是“空的”,通过 PHP 设计。这是对“空”概念的滥用恕我直言:单个数字不是集合,因此“空”不能合理地使用 0。这只会导致编程晦涩,因为它违反了最小惊喜原则。我敢肯定 PHP 设计者会认为“零是空数”作为某种模糊的类比;但是如果类比是模糊的,为什么要费心呢?但是 PHP 充满了愚蠢的想法。
回答by DEVARAJ JOHNSON
The table below is an easy reference for what these functions will return for different values. The blank spaces means the function returns bool(false).
下表是这些函数针对不同值返回的内容的简单参考。空格表示函数返回 bool(false)。
refer this link for more https://www.virendrachandak.com/techtalk/php-isset-vs-empty-vs-is_null/
有关更多信息,请参阅此链接 https://www.virendrachandak.com/techtalk/php-isset-vs-empty-vs-is_null/
回答by Chris
NULL is a special value which explicitly states that the variable has not been set to any value yet. Be careful with using the empty()
function as you can't just determine that a variable is exactly NULL using it. For example the empty()
function will return true if an int is set to 0. If you need to make sure a variable is exactly NULL
use if($variable == NULL)
.
NULL 是一个特殊值,它明确指出变量尚未设置为任何值。使用该empty()
函数时要小心,因为您不能使用它来确定一个变量完全为 NULL。例如,empty()
如果 int 设置为 0,该函数将返回 true。如果您需要确保一个变量完全正确,请NULL
使用if($variable == NULL)
。
For more info on empty()
see http://php.net/manual/en/function.empty.php
有关更多信息,empty()
请参见http://php.net/manual/en/function.empty.php
回答by Andrew Cooper
There are some good answers here, which I won't repeat. In the case of validating forms, though, when a form is submitted, the value of each form input element is sent to the server in the $_POST
variable. You can check for the existence of a particular input by using isset()
.
这里有一些很好的答案,我不会重复。但是,在验证表单的情况下,当提交表单时,每个表单输入元素的值都会在$_POST
变量中发送到服务器。您可以使用 来检查特定输入是否存在isset()
。
isset($_POST['username'])
If this returns true, then this request to the server was the result of posting a form containing an input element named "username". Now that we know that we have a value for that form element, we can see if it has a valid value. empty()
will tell us whether the user actually entered any data in the field, or whether they left it empty.
如果返回 true,则此对服务器的请求是发布包含名为“username”的输入元素的表单的结果。现在我们知道我们有一个表单元素的值,我们可以看看它是否有一个有效的值。 empty()
将告诉我们用户是否实际在该字段中输入了任何数据,或者是否将其留空。
empty($_POST['username'])
If that returns true then the form submitted to the server had a field named "username" but the user didn't enter anything into before submitting the form.
如果返回 true 则提交给服务器的表单有一个名为“用户名”的字段,但用户在提交表单之前没有输入任何内容。
回答by pmobley
The empty()
is a nice fast way to see if the variable holds any useful info... that is for strings empty()
returns true for a string of "" as well as a null string.
这empty()
是查看变量是否包含任何有用信息的一种很好的快速方法......即对于字符串,empty()
对于字符串“”以及空字符串返回true。
So you can write something like this:
所以你可以这样写:
if (! empty($name)) echo $name;
More info see here: PHP: empty()
更多信息请参见此处:PHP:empty()
回答by Khez
isset()
returns true if both these conditions are met:
isset()
如果满足这两个条件,则返回 true:
- The variable has been defined and has not yet been unset.
- The variable has a non-null value in it.
- 变量已定义且尚未取消设置。
- 变量中有一个非空值。
A variable is automatically defined when it gets set to something (including null). This has a direct implication in arrays.
当一个变量被设置为某些东西(包括空值)时,它会自动定义。这对数组有直接的影响。
$a=array();
$a['randomKey']=true;
$a['nullKey']=null;
var_dump(isset($a['randomKey'])); // true
var_dump(isset($a['nullKey'])); // true, the key has been set, set to null!
var_dump(isset($a['unsetKey'])); // false !
unset($a['randomKey']);
var_dump(isset($a['randomKey'])); // false ! it's been unset!
From above, you can check if various $_POST fields have been set. For example, a page that has been posted to, stands to reason, has the submit button name in the $_POST field.
从上面,您可以检查是否设置了各种 $_POST 字段。例如,一个已被张贴到的页面,按理说,在 $_POST 字段中具有提交按钮名称。
empty()
on the other hand, tests if the variable holds a non zero value. This means that values that (int) cast to 0, return false too. You can use this to see if a specific $_POST field has data in it.
empty()
另一方面,测试变量是否保持非零值。这意味着 (int) 转换为 0 的值也会返回 false。您可以使用它来查看特定的 $_POST 字段中是否包含数据。
回答by Khez
This concept can be better understood from mathematics. Have you ever tried dividing a number (not zero) by 0 using a calculator e.g 7/0? You will get a result that looks like something this: undefined
, not a number
, null
etc. This means that the operation is impossible, for some reasons (let's leave those reasons to be discussed another day).
这个概念可以从数学中更好地理解。您是否尝试过使用计算器(例如 7/0)将数字(非零)除以 0?你会得到一个结果,看起来像这样的东西:undefined
,not a number
,null
等等。这意味着操作是不可能的,因为某些原因(让我们离开这些原因将要讨论的另一天)。
Now, perform this: 0/7. You will get the output, 0. This means that the operation is possible and can be executed, but you the answer is just 0 because nothing is left after the division. There is a valid output and that output is zero.
现在,执行此操作:0/7。您将获得输出 0。这意味着该操作是可能的并且可以执行,但您的答案只是 0,因为除法后什么也没有留下。有一个有效的输出并且该输出为零。
In the first example, not only was the output invalid, the operation was not possible to execute. This is akin to null
. The second example is akin to empty
.
在第一个示例中,不仅输出无效,操作也无法执行。这类似于null
. 第二个例子类似于empty
.
回答by SpliFF
Been awhile since i used PHP but if other languages are anything to go by empty will indicate an existing object/map/array that has no contents while null would indicate a variable that has no meaning/definition at all (uninitialised).
自从我使用 PHP 以来已经有一段时间了,但如果其他语言是空的,则表示没有内容的现有对象/映射/数组,而 null 表示根本没有意义/定义的变量(未初始化)。
In database SQL, NULL means "no value".
在数据库 SQL 中,NULL 表示“没有值”。