码迷,mamicode.com
首页 > Web开发 > 详细

php != 和 !== 的区别

时间:2016-06-02 19:44:54      阅读:275      评论:0      收藏:0      [点我收藏+]

标签:

== and != do not take into account the data type of the variables you compare. So these would all return true:

‘0‘   == 0
false == 0
NULL  == false

=== and !== do take into account the data type. That means comparing a string to a boolean willnever be true because they‘re of different types for example. These will all return false:

‘0‘   === 0
false === 0
NULL  === false

You should compare data types for functions that return values that could possibly be of ambiguous truthy/falsy value. A well-known example is strpos():

// This returns 0 because F exists as the first character, but as my above example,
// 0 could mean false, so using == or != would return an incorrect result
var_dump(strpos(‘Foo‘, ‘F‘) != false);  // bool(false)
var_dump(strpos(‘Foo‘, ‘F‘) !== false); // bool(true), it exists so false isn‘t returned

转自: http://stackoverflow.com/questions/3641819/php-not-equal-to-and

php != 和 !== 的区别

标签:

原文地址:http://www.cnblogs.com/pinganzi/p/5553987.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!