0
A
回答
1
在这种情况下它是-1
被转换为布尔型(true
,因为只有如0
false
进行处理)。所以最终的比较是
if (true > false) {
...
}
Type Juggling可以非常直观,所以总是尽量避免在那里你比较两个不同类型的变量的情况。在平等比较的情况下,总是尝试使用身份运算符(===
),如果出现不平等情况,您只能添加手动投射。
1
<
是数值比较运算符,代码没有松散比较转换-1
为真,因此结果。
2
请参阅PHP Comparision Operators - 表各类
Type of Operand 1 Type of Operand 2 Result
bool or null anything Convert both sides to bool, FALSE < TRUE
所以,如果你比较布尔其他任何事情,那么第二个操作数强制转换为布尔值true比较。 我们还有这里的信息,那FALSE < TRUE
,在你的例子中究竟发生了什么。
1
WARNING: -1 is considered TRUE, like any other non-zero
(whether negative or positive) number!
请检查这两个代码。你可以得到的区别
if(-1 > false)
print "Here"; //This will print the `Here`
if(-1 > 0)
print "Here"; // Not print `Here`
+0
为什么地狱应该-1被认为是假? – fast
相关问题
- 1. 为什么console.log(true && undefined)返回undefined而if(true && undefined)返回false?
- 2. anyDensity = false为什么getDrawingCache(true)返回null?
- 3. 为什么JS中的true =='true'语句返回false?
- 4. 为什么preg_match()代码在PHP CLI中返回true而在Apache中为false
- 5. 只返回true或false(PHP)
- 6. 为什么布尔函数返回false返回true
- 7. Python:为什么isinstance返回False,何时应该返回True?
- 8. 为什么包括?方法在dev中返回true,在Heroku中返回false?
- 9. 为什么Html.Checkbox(“可见”)在ASP.NET MVC 2中返回“true,false”?
- 10. 为什么(null == false)和(null == true)都返回false?
- 11. PHP如果为True返回false
- 12. Boolean.getBoolean(“true”)返回false
- 13. 为什么Request.IsSecureConnection在预期值为true时返回false
- 14. 为什么此条件返回false,但在主函数中返回true?
- 15. 为什么PHP的zip_read()返回false?
- 16. 返回true或false?
- 17. ajax返回true,但==返回false
- 18. 为什么在返回TRUE/FALSE到布尔类型时出错?
- 19. 为什么在ActionMode中isItemChecked()返回true?
- 20. 为什么==返回true?
- 21. 为什么它返回true?
- 22. 为什么hasOwnProperty返回true
- 23. 为什么String.matches在Java中返回false?
- 24. 为什么instanceof在JavaScript中返回false?
- 25. 为什么== Integer.valueOf(500)返回false,但是true为5?
- 26. 为什么string.contains()返回false?
- 27. jquery返回false - 为什么?
- 28. 为什么setFieldProperty返回false?
- 29. Properties.containsKey返回false,为什么?
- 30. 为什么File.Exists返回false?
'(INT)假== 0'我认为 – Exlord
从文档:'警告:-1被认为是TRUE,像任何其他非零(不论正负)数! 'http://www.php.net/manual/en/language.types.boolean.php –
它有关系吗?没有人写这样的代码。 –