空和空有什么区别?

发布于 2024-10-31 11:57:36 字数 320 浏览 2 评论 0原文

我对空和空的概念很陌生。虽然我努力理解它们之间的区别,但我更困惑。我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

傾城如夢未必闌珊 2024-11-07 11:57:36

如果变量没有值并且指向内存中的任何位置,则该变量为 NULL。

empty() 更多的是empty的字面意思,例如字符串""是空的,但是not NULL

考虑以下事项
为空:

  • ""(空字符串)
  • 0(0 作为整数)
  • 0.0(0 作为浮点数)
  • “0”(0 作为字符串)
  • 错误
  • array()(空数组)
  • var $var; (声明了一个变量,但在类中没有值)


示例

$aNULL

$a = '',但不是NULL


更新

如果$a=''为空但不是NULL,我什么时候使用empty()函数以及什么时候使用isset() 函数。

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 not 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)

Source.


Example

$a is NULL.

$a = '' is empty, but not NULL.


Update

If $a='' is empty but not NULL, when do I use the empty() function and when do I use the isset() function.

isset() will return FALSE is the variable is pointing to NULL.

Use empty() when you understand what is empty (look at the list above).

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.

If it were $str = NULL, it would not occupy any memory.

眼泪也成诗 2024-11-07 11:57:36

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.

心碎无痕… 2024-11-07 11:57:36

下表是这些函数针对不同值返回的内容的简单参考。空格表示函数返回 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).
enter image description here

refer this link for more https://www.virendrachandak.com/techtalk/php-isset-vs-empty-vs-is_null/

枕花眠 2024-11-07 11:57:36

NULL 是一个特殊值,它明确表明该变量尚未设置为任何值。使用 empty() 函数时要小心,因为您不能仅使用它来确定变量是否为 NULL。例如,如果 int 设置为 0,empty() 函数将返回 true。如果需要确保变量恰好为 NULL,请使用 if($变量== NULL)

有关 empty() 的更多信息,请参阅 http://php. net/manual/en/function.empty.php

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).

For more info on empty() see http://php.net/manual/en/function.empty.php

倒数 2024-11-07 11:57:36

这里有一些很好的答案,我不再重复。但是,在验证表单的情况下,当提交表单时,每个表单输入元素的值都会通过 $_POST 变量发送到服务器。您可以使用 isset() 检查特定输入是否存在。

isset($_POST['username'])

如果返回 true,则对服务器的此请求是发布包含名为“username”的输入元素的表单的结果。现在我们知道该表单元素有一个值,我们可以查看它是否具有有效值。 empty() 将告诉我们用户是否实际在字段中输入了任何数据,或者是否将其留空。

empty($_POST['username'])

如果返回 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 using 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.

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.

再可℃爱ぅ一点好了 2024-11-07 11:57:36

自从我使用 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".

顾冷 2024-11-07 11:57:36

empty() 是查看变量是否包含任何有用信息的快速方法...对于字符串 empty() 对于字符串“”返回 true以及一个空字符串。

所以你可以写这样的东西:

if (! empty($name)) echo $name;

更多信息请参见这里:PHP:empty()

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.

So you can write something like this:

if (! empty($name)) echo $name;

More info see here: PHP: empty()

花辞树 2024-11-07 11:57:36

如果满足这两个条件,则 isset() 返回 true:

  1. 变量已定义且尚未取消设置。
  2. 该变量中有一个非空值。

当变量被设置为某个值(包括 null)时,它会自动定义。这对数组有直接的影响。

$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!

从上面,您可以检查是否已设置各个 $_POST 字段。例如,一个已发布的页面按理说在 $_POST 字段中具有提交按钮名称。

另一方面,empty() 测试变量是否包含非零值。这意味着 (int) 转换为 0 的值也会返回 false。您可以使用它来查看特定的 $_POST 字段中是否有数据。

isset() returns true if both these conditions are met:

  1. The variable has been defined and has not yet been unset.
  2. 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.

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.

高跟鞋的旋律 2024-11-07 11:57:36

这个概念可以从数学中更好地理解。您是否尝试过使用计算器(例如 7/0)将数字(非零)除以 0?您将得到如下所示的结果:undefinednot a numbernull 等。这意味着该操作是不可能的,因为一些原因(让我们改天再讨论这些原因)。

现在,执行以下操作: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 to empty.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文