我可以使用两个||和一个&&在PHP中的同一个if语句中?

时间:2017-11-04 05:07:21

标签: php if-statement logical-operators

在选择器选项中选择两个行星A和B来测量距离。

例如:

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"; 
}

您从Nova Terra到Nova Iaponia或从Nova Iaponia到Nova Terra"阅读。 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 个答案:

答案 0 :(得分: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"; 
}