2010-08-30 20 views
1

哪一項是正確的語法正確的語法不等於操作這是本comparsion操作

本 - > =

if($ses_startdate != $startdate) { 
       echo "I am true"; 
      } 

或本! - > ==

if($ses_startdate !== $startdate) { 
       echo "I am true"; 
      } 

我一直在使用!==早些時候,它的工作沒有任何問題,但不是它創造了一些條件的問題,當我改變它!=它工作正常..爲什麼?

回答

7

!==!=更嚴格,!==也檢查數據類型。 例子:

$a = 1; 
$b = '1'; 
$c = 1; 
$d = TRUE; 
// These are true: 
$a == $c; 
$a == $b; 
$a === $c 
$a == $d; 
// but these are FALSE: 
$a === $b; 
$a === $d; 
+1

http://php.net/manual/en/language.operators.comparison.php – 2010-08-30 14:51:17

+0

對於某些條件,我測試了它,然後即使兩個參數的值相等,它仍用於輸入條件,而我有如果是的話,只提到輸入!==。 – 2010-08-30 14:52:07

+0

我假設最後一個例子應該是'$ a === $ b'(否則就是'true')...否則,+1 ... – ircmaxell 2010-08-30 14:53:09

1
$a = ''; 
$b = false; 

if($a != $b) 
    //it is not executed since $a and $b are the same (empty) but have different types. 

if($a !== $b) 
    //it is executed, because $a is a string and $b is boolean, even though both of them represent the same value (empty). 

因此,第三=告訴PHP來檢查類型了。

1

$ses_startdate != $startdate

在做這fork了你的代碼進行任何比較,習慣做的

var_dump ($ses_startdate);

var_dump ($startdate);

只是在此之前,條件等同,自卸值設置到頁面或將其放入您的error_logs中,以便您可以看到PHP在評估變量的類型和值。

+0

+ 1的信息。 – 2010-08-30 15:08:39