2010-06-03 58 views
1
if(0 == ('Pictures')) 
{ 
    echo 'true'; 
} 

为什么它给我'真'?PHP比较疑问

+1

http://php.net/manual/en/language。 operators.comparison.php – nuqqsa 2010-06-03 10:05:12

回答

2

检查PHP type comparison tables了解比较运营商在PHP中如何表现。

在你的情况, '图片' 变为 “0”,因此0 = 0

让我们来看看下面的例子:

echo (int)'Pictures'; // 0 => 'Picture' as int 
echo 0 == 'Pictures'; // 1 => true, 0 = 0 
3

你的字符串将被评估为一个整数,所以变为0时,使用这样的:0 === 'Pictures'用于验证身份(相同值和相同类型)

0

使用:

if (0 === 'Pictures') 
{ 
    echo 'true'; 
} 

===是严格的类型操作,它不仅检查值,而且检查类型。

快速测试:

if(0 == 'Pictures') 
{ 
    echo 'true'; 
} 
else 
{ 
    echo 'false'; 
} 

输出true但:

if(0 === 'Pictures') 
{ 
    echo 'true'; 
} 
else 
{ 
    echo 'false'; 
} 

输出false