2014-03-25 140 views
0
<?php 
    var_dump($isHoster); // prints int(0) 

    if ($isHoster == 'all') 
     $conditionsHoster = '0, 1'; 
    else 
     $conditionsHoster = intval($isHoster); 

    var_dump($conditionsHoster); // prints string(4) "0, 1" 
?> 

这是怎么回事?谁能解释这一点? 这从来没有发生在我身上......简单如果其他不工作

+1

相关手册文档:http://php.net/manual/en/language.types.string.php#language.types.string.conversion –

+0

为什么? “all”永远不可能与0相同。 – user1711384

+1

它没有任何意义,但这就是PHP如何做到的。比较相同类型的东西或使用'==='来代替比较安全。 –

回答

6

0 == 'all'因为PHP尝试'all'转换为int(int) 'all'0是在PHP真实的;你应该写

if ($isHoster === 'all') 
1
var_dump($isHoster); // prints int(0) 

    if ($isHoster === 'all') 
     $conditionsHoster = '0, 1'; 
    else 
     $conditionsHoster = intval($isHoster); 

    var_dump($conditionsHoster); 

它,因为你是用字符串比较诠释。 在比较转换$ isHoster之前,比如$ isHoster =(string)$ isHoster;或用户===进行比较。