PHP 认为 null 等于零
在 PHP 中,($myvariable==0)
当$myvariable为零时,表达式的值为true;当 $myvariable 为 null 时,该表达式的值也为 true。如何排除第二种情况呢?我的意思是我希望仅当 $myvariable 为零时表达式才为真。我当然可以写
($myvariable != null && $myvariable == 0)
但是还有其他优雅的方式来做到这一点吗?
In PHP, ($myvariable==0)
When $myvariable is zero, the value of the expression is true; when $myvariable is null, the value of this expression is also true. How can I exclude the second case? I mean I want the expression to be true only when $myvariable is zero. Of course I can write
($myvariable != null && $myvariable == 0)
But is there other elegant way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
详细了解比较运算符。
read more about comparison operators.
你暗示了一个深刻的问题:表达式什么时候应该为真?
下面,我将解释为什么您所做的事情不起作用以及如何解决它。
在许多语言中,
null
、0
和空字符串 (""
) 的计算结果都为 false,这可以使if 语句非常简洁直观,但
null
、0
和""
也都是不同的类型。他们应该如何比较?此页面告诉我们,如果要比较两个变量,那么变量将按如下方式转换(在第一个匹配处退出表)
因此您正在比较 null 与数字。因此,null 和数字都会转换为布尔值。 此页面告诉我们,在这样的转换中
null
和0
被视为FALSE
。现在,您的表达式为
false==false
,这当然是 true。但不是你想要的。
此页面提供了 PHP 比较运算符的列表。
第一个比较器是您现在使用的比较器。请注意,它执行我之前提到的转换。
使用第二个比较器将解决您的问题。由于 null 和数字不是同一类型,因此
===
比较将返回 false,而不是像==
运算符那样执行类型转换。希望这有帮助。
You hint at a deep question: when should an expression be true?
Below, I will explain why what you are doing isn't working and how to fix it.
In many languages
null
,0
, and the empty string (""
) all evaluate to false, this can makeif
statements quite succinct and intuitive, butnull
,0
, and""
are also all of different types. How should they be compared?This page tells us that if we have two variables being compared, then the variables are converted as follows (exiting the table at the first match)
So you are comparing a null versus a number. Therefore, both the null and the number are converted to boolean. This page tells us that in such a conversion both
null
and0
are consideredFALSE
.Your expression now reads,
false==false
, which, of course, is true.But not what you want.
This page provides a list of PHP's comparison operators.
The first comparator is the comparison you are using now. Note that it performs the conversions I mentioned earlier.
Using the second comparator will fix your problem. Since a null and a number are not of the same type, the
===
comparison will return false, rather than performing type conversion as the==
operator would.Hope this helps.
通过以下方式标识为 null 或零:
is_int($var)
。要识别零,使用is_numeric($var)
也是解决方案或使用$var === 0
is_null($var)
> 如果变量为 NULLTo identify as null or zero by:
is_int($var)
if a variable is a number or a numeric string. To identify Zero, useis_numeric($var)
is also the solution or use$var === 0
is_null($var)
if a variable is NULL尝试
($myvariable === 0)
,它不会执行类型强制。Try
($myvariable === 0)
which will not perform type coercion.使用 php 函数 is_null( ) 函数以及 <代码>=== 运算符。
!==
也可以按照您期望的方式工作。Use the php function is_null( ) function along with the
===
operator.!==
also works the way you'd expect.第二种解决方案也行不通。
===
运算符是您问题的解决方案。The second solution wouldn't work either. The
===
operator is the solution to your problem.如果你的零可能是一个字符串,你还应该考虑检查“零字符串”
If your zero could be a string, you should also considere checking the "zero string"
我在我的一个项目中遇到了类似的问题,有一个细微的差别,即我还使用值 ZERO 作为我的条件的有效值。以下是我如何使用简单的逻辑将 NULL 与零和其他值分开来解决这个问题。
I hade faced a similar issue in one of my projects, with a minor difference that I was also using the values ZERO as a valid value for my condition. here's how I solved it using simple logic to separate NULL from zero and other values.
如果 $a 等于 $b,并且它们属于相同类型,则相同 TRUE
Identical TRUE if $a is equal to $b, and they are of the same type
有一个
is_null
函数,但是这个只会替换您的$myvariable!=null
There's an
is_null
function, but this will just replace your$myvariable!=null
对于我的情况,我找到了这个解决方案,它对我有用:
For my case i found this soulution and it works for me :