php - null 和 empty 和有什么不一样?

标签 php null isset

我对 empty 和 null 的概念不熟悉。虽然我努力理解它们之间的区别,但我更加困惑。我在 http://www.tutorialarena.com/blog/php-isset-vs-empty.php 看到一篇文章但是我仍然看不到在验证表单时何时使用 isset 和 empty 。看我没把握到区别,我不想用错功能,也不想用不上其他地方的功能。有人可以举出有助于我理解的例子吗?我是编码的新手,所以如果有人能给我真实世界的例子,同时保持它足够简单,让新手能够理解,我将不胜感激。

最佳答案

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

empty() 更多的是 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)

Source .


例子

$aNULL

$a = '',但不是NULL


更新

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

Also when you say it points nowhere in memory, what does that mean exactly?

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

如果它是$str = NULL,它不会占用任何内存。

关于php - null 和 empty 和有什么不一样?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5615747/

相关文章:

php - $_SERVER ['REQUEST_METHOD' ] 默认计算为 'GET'

php - 出现任何错误时停止并返回 Json 响应

javascript - 检查JS中是否设置了数组项

PHP:使用 usort() 时,数组项如何传递给值比较函数?

php - 警告 : Illegal offset type while create dyamic menu

c# - 为什么对于那些喜欢类型安全的人来说 Null 不是类型安全的无关紧要

linux - linux malloc 的意外行为

python - 尝试从 Pandas 数据框中以字符串开头的所有列中选择数据

javascript - 在 javascript/jQuery 中有类似 isset of php 的东西吗?

php - 即使有 isset 也未定义索引