[英]Unexpected PHP behaviour in isset and empty
array(4) { ["id"]=> int(0) ["pass"]=> string(0) "" ["log_in"]=> string(3) "no" }
The problem in id
when using isset
it return true
because it's 0
. 使用
isset
时id
的问题返回true
因为它是0
。 When using empty I think it's doing the same. 当使用空时,我认为它也是一样。 What is the best function to know whether it's set or not?
知道是否已设置的最佳功能是什么?
empty
returns false for these: empty
返回这些错误:
""
(an empty string) ""
(空字符串) 0
(0 as an integer) 0
(0为整数) "0"
(0 as a string) "0"
(0作为字符串) NULL
FALSE
array()
(an empty array) array()
(一个空数组) var $var
(a variable declared, but without a value in a class) var $var
(已声明变量,但类中没有值) You need to use isset
instead to check if it is there. 您需要使用
isset
来检查它是否存在。
See this article on empty vs isset for more info. 有关更多信息,请参见这篇关于empty vs isset的文章。
if($array['id'] === 0) {
true
} ?
a simple if condition will do for you 一个简单的条件是否适合您
if(id) { }
try it if it works. 如果可行,请尝试。
if id is a database id is positive integer 如果id是数据库id是正整数
so if($id>0)
is one simple solution 所以
if($id>0)
是一个简单的解决方案
OR 要么
preg_match('#^[0-9]*$#', $id)
another 另一个
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.