2017-11-04 119 views
6

您在選擇器選項中選擇兩個行星A和B來測量距離。我可以使用兩個||嗎?和一個&&在PHP相同的if語句?

例如:

if (($planetA == "Nova Terra" || $planetB == "Nova Iaponia") && ($planetA == "Nova Iaponia" || $planetB == "Nova Terra")) 
{ 
    echo "From $planetA to $planet B: 290 parsecs"; 
} 

else if (($planetA == "Nova Terra" || $planetB == "Novo Mars") && ($planetA == "Novo Mars" || $planetB == "Nova Terra")) 
{ 
    echo "From $planetA to $planet B: 230 parsecs"; 
} 

你讀「從新星到泰拉諾娃Iaponia或從新星Iaponia新星兵馬俑」。 To =和。

這是類似這樣的:

if ($planetA == "Nova Terra" || $planetB == "Nova Iaponia") 
{ 
    echo "From $planetA to $planet B: 290 parsecs"; 
} 

else if ($planetA == "Nova Iaponia" || $planetB == "Nova Terra") 
{ 
    echo "From $planetA to $planet B: 290 parsecs"; 
} 

else if ($planetA == "Nova Terra" || $planetB == "Novo Mars") 
{ 
    echo "From $planetA to $planet B: 230 parsecs"; 
} 

else if ($planetA == "Novo Mars" || $planetB == "Nova Terra") 
{ 
    echo "From $planetA to $planet B: 230 parsecs"; 
} 
+1

你「能」做到這一點(它的語法有效),但邏輯錯了。如果planetA和planetB都是「Nova Terra」,那麼您會收到290條短信。 –

+0

這是我所指的問題。將正確的邏輯分成4條語句會分裂嗎?但我想縮短和節約代碼。 –

回答

4

你只需要交換你的邏輯運算符來獲得結果你想要

if (($planetA == "Nova Terra" && $planetB == "Nova Iaponia") || ($planetA == "Nova Iaponia" && $planetB == "Nova Terra")) 
{ 
    echo "From $planetA to $planet B: 290 parsecs"; 
} 

else if (($planetA == "Nova Terra" && $planetB == "Novo Mars") || ($planetA == "Novo Mars" && $planetB == "Nova Terra")) 
{ 
    echo "From $planetA to $planet B: 230 parsecs"; 
} 
相關問題