null 和 empty 和有什么不一样?

2022-01-06 00:00:00 null php isset

我对空和空的概念很陌生.虽然我已经努力了解它们之间的区别,但我更加困惑.我在 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.

推荐答案

一个变量是 NULL 如果它没有值,并且不指向内存中的任何地方.

A variable is NULL if it has no value, and points to nowhere in memory.

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

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:

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

来源.

$aNULL.

$a = '' 是 empty,但不是 NULL.

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

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

isset() 将返回 FALSE 是指向 NULL 的变量.

当您了解什么是 empty 时,请使用 empty()(查看上面的列表).

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?

这意味着 $str = '' 将作为长度为 0 的字符串存在于内存中.

It means that $str = '' will be in memory as a string with length of 0.

如果是$str = NULL,就不会占用内存.

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

相关文章