我对 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 .
例子
$a
为 NULL
。
$a = ''
是空,但不是NULL
。
更新
If
$a=''
is empty but notNULL
, when do I use theempty()
function and when do I use theisset()
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/