2014-09-23 9 views
0

我無法在任何地方找到答案,這是一個新的錯誤。有人可以幫我這個說法:正確使用AND OR(|| &&)和兩個字符串值

<?php 
if ($db_ownrPlan == 'sell in a year or so' || 'get some info only no plans made yet'){ 
    echo "$TYmessage"; 
} elseif ($db_ownrPlan == 'sell soon' || 'sell in 3 to 6 months' || 'looking to buy this property'){ 
    echo "$TYmessage1"; 
} else { 
    echo "$TYmessage2"; 
} 
?> 
+1

'$變量==「value''是它返回一個'boolean'值(TRUE;比較或「假」)。 ''只獲取一些信息,尚未制定計劃'是一個字符串,不會返回**布爾值**。你必須再做一次比較:'if($ db_ownrPlan =='在一年左右賣出'|| $ db_ownrPlan =='只獲得一些信息,沒有計劃')''。 '&&'和'||'操作符組合邏輯(布爾)值以返回另一個布爾值。 – 2014-09-23 23:54:12

+0

你找不到答案嗎?真?來吧,PHP文檔本身教你如何編寫條件語句。什麼阻止你閱讀它? – 2014-09-23 23:55:11

+2

@AlejandroIván:字符串可以轉換爲布爾值就好了。這不僅僅是你想要在這裏發生的事情(坦率地說,在任何地方)。 – 2014-09-23 23:56:18

回答

0

這是你想要做什麼:

<?php 
if ($db_ownrPlan == 'sell in a year or so' || $db_ownrPlan == 'get some info only no plans made yet'){ 
    echo "$TYmessage"; 
} elseif ($db_ownrPlan == 'sell soon' || $db_ownrPlan == 'sell in 3 to 6 months' || $db_ownrPlan == 'looking to buy this property'){ 
    echo "$TYmessage1"; 
} else { 
    echo "$TYmessage2"; 
} 
?> 

在PHP中,並用類似的語法大多數其他編程語言,你會比較喜歡這樣的變量:

VAR == value OR VAR == otherValue 
1

正確的語法是:

($db_ownrPlan == 'sell in a year or so' || $db_ownrPlan =='get some info only no plans made yet') 

否則第二個條件僅僅是字符串,而不是比較。

2
if (in_array($db_ownrPlan, 
    array('sell in a year or so', 'get some info only no plans made yet'))) 

等了緊湊型(沒有檢查速度)...