空和空有什么区别?
我对空和空的概念很陌生。虽然我努力理解它们之间的区别,但我更困惑。我在 http://www.tutorialarena.com/blog/php-isset- 看到一篇文章vs-empty.php 但是我仍然不知道在验证表单时什么时候会使用 isset 和empty。看到我不明白其中的区别,我不想使用不正确的功能,也不想无法使用其他区域的功能。有人可以举一些例子来帮助我理解吗?我对编码非常陌生,因此如果有人能给我提供真实世界的示例,同时保持其足够简单以供菜鸟遵循,我将不胜感激。
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.php however 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
如果变量没有值并且指向内存中的任何位置,则该变量为 NULL。
empty()
更多的是empty的字面意思,例如字符串""
是空的,但是notNULL
。源。
示例
$a
为NULL
。$a = ''
是空,但不是NULL
。更新
isset()
将返回FALSE< /code> 是指向
NULL
的变量。当您了解什么是空时,请使用
empty()
(请查看上面的列表)。这意味着
$str = ''
将以长度为0的字符串形式存在于内存中。如果是
$str = NULL
,则不会占用任何内存。A variable is
NULL
if it has no value, and points to nowhere in memory.empty()
is more a literal meaning of empty, e.g. the string""
is empty, but is notNULL
.Source.
Example
$a
isNULL
.$a = ''
is empty, but notNULL
.Update
isset()
will returnFALSE
is the variable is pointing toNULL
.Use
empty()
when you understand what is empty (look at the list above).It means that
$str = ''
will be in memory as a string with length of 0.If it were
$str = NULL
, it would not occupy any memory.Null 是一个占位符,通常表示“没有可用的相关数据”。
为此使用 null 只是一种约定,但相当广泛,以至于某些编程语言直接支持该约定。恕我直言,这个约定存在的原因在历史上与“指针”有关。
很多时候,过程被定义为返回指向答案的指针,并且如果由于某种原因无法产生答案,则将返回传统上称为空指针的内容。
空意味着(如果这是一个集合)它没有成员。这是一个明确的答案,它与“没有可用的数据”非常不同。
在 PHP 世界中,显然未初始化的变量具有 Null 值,并且在此类变量上设置 isset 返回 FALSE。
对于数组和字符串,PHP 遵循“空”表示“没有成员”的约定,尽管数组和字符串在技术上不是集合。
PHP 显然有一个有趣的想法,即 0 和 0.0 也是“空”,这是 PHP 设计的。恕我直言,这是对“空”概念的滥用:单个数字不是集合,因此 0 不能合理地“空”。这只会导致晦涩的编程,因为它违反了最小惊喜原则。我确信 PHP 设计者会认为“零是空数”是某种模糊的类比;但如果这个类比是模糊的,为什么还要费心呢?但 PHP 充满了愚蠢的想法。
Null is a placeholder that generally means "no data about this is available".
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.
Empty means (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 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.
下表是这些函数针对不同值返回的内容的简单参考。空格表示函数返回 bool(false)。
请参阅此链接了解更多信息 https://www.virendrachandak.com/techtalk/php-isset-vs-empty-vs-is_null/
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).
refer this link for more https://www.virendrachandak.com/techtalk/php-isset-vs-empty-vs-is_null/
NULL 是一个特殊值,它明确表明该变量尚未设置为任何值。使用
empty()
函数时要小心,因为您不能仅使用它来确定变量是否为 NULL。例如,如果 int 设置为 0,empty()
函数将返回 true。如果需要确保变量恰好为NULL
,请使用if($变量== NULL)
。有关
empty()
的更多信息,请参阅 http://php. net/manual/en/function.empty.phpNULL 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 theempty()
function will return true if an int is set to 0. If you need to make sure a variable is exactlyNULL
useif($variable == NULL)
.For more info on
empty()
see http://php.net/manual/en/function.empty.php这里有一些很好的答案,我不再重复。但是,在验证表单的情况下,当提交表单时,每个表单输入元素的值都会通过
$_POST
变量发送到服务器。您可以使用isset()
检查特定输入是否存在。如果返回 true,则对服务器的此请求是发布包含名为“username”的输入元素的表单的结果。现在我们知道该表单元素有一个值,我们可以查看它是否具有有效值。
empty()
将告诉我们用户是否实际在字段中输入了任何数据,或者是否将其留空。如果返回 true,则提交给服务器的表单有一个名为“用户名”的字段,但用户在提交表单之前没有输入任何内容。
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 usingisset()
.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.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.
自从我使用 PHP 以来已经有一段时间了,但如果其他语言有任何东西,则 null 将指示一个没有内容的现有对象/映射/数组,而 null 将指示一个根本没有含义/定义的变量(未初始化)。
在数据库SQL中,NULL表示“无值”。
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).
In database SQL, NULL means "no value".
empty()
是查看变量是否包含任何有用信息的快速方法...对于字符串empty()
对于字符串“”返回 true以及一个空字符串。所以你可以写这样的东西:
更多信息请参见这里:PHP:empty()
The
empty()
is a nice fast way to see if the variable holds any useful info... that is for stringsempty()
returns true for a string of "" as well as a null string.So you can write something like this:
More info see here: PHP: empty()
如果满足这两个条件,则
isset()
返回 true:当变量被设置为某个值(包括 null)时,它会自动定义。这对数组有直接的影响。
从上面,您可以检查是否已设置各个 $_POST 字段。例如,一个已发布的页面按理说在 $_POST 字段中具有提交按钮名称。
另一方面,
empty()
测试变量是否包含非零值。这意味着 (int) 转换为 0 的值也会返回 false。您可以使用它来查看特定的 $_POST 字段中是否有数据。isset()
returns true if both these conditions are met:A variable is automatically defined when it gets set to something (including null). This has a direct implication in arrays.
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.
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.这个概念可以从数学中更好地理解。您是否尝试过使用计算器(例如 7/0)将数字(非零)除以 0?您将得到如下所示的结果:
undefined
、not a number
、null
等。这意味着该操作是不可能的,因为一些原因(让我们改天再讨论这些原因)。现在,执行以下操作:0/7。您将得到输出 0。这意味着该操作是可能的并且可以执行,但您的答案只是 0,因为除法后什么都没有留下。存在有效输出且该输出为零。
在第一个示例中,不仅输出无效,而且操作也无法执行。这类似于
null
。第二个示例类似于empty
。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).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.
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 toempty
.