简体   繁体   English

如果PHP中的或语句无法正确评估

[英]If or statement in php not evaluating correctly

I have this PHP statement: 我有这个PHP语句:

if (($row['rest'] != "") or ($row['rest'] != "Select An Option")) {
    $rest = "<b>Rest Stops:</b> {$row['rest']},";
}
else {
    $rest = "";
}

which is not evaluating properly and I can't figure out why. 评估不正确,我不知道为什么。 What I want the statement to do is if the field 'rest' is blank or "Select An Option" then the variable $rest should evaluate to "Rest Stops:" followed by the data. 我要该语句执行的操作是,如果字段“ rest”为空白或“选择选项”,则变量$ rest的值应为“ Rest Stops:”,后跟数据。 My data is "Select An Option" and I get "Rest Stops: Select An Option" as the output. 我的数据是“选择一个选项”,并且我得到“停靠站:选择一个选项”作为输出。 I did some testing of this statement and I figured out PHP is assigning the variable $row['rest'] as not equal to "" instead of evaluating the 'or' statement. 我对该语句进行了一些测试,发现PHP正在将变量$ row ['rest']分配为不等于“”,而不是评估“ or”语句。 What would be the correct syntax? 正确的语法是什么?

What I want the statement to do is if the field 'rest' is blank or Select An Option than the variable $rest should evaluate to Rest Stops: followed by the data. 我要该语句执行的操作是,如果字段“ rest”为空白或Select An Option ,则变量$rest应为Rest Stops:后跟数据。

Your logic is incorrect. 您的逻辑不正确。 You need to check if they are equal to , so use == instead of != . 您需要检查它们是否相等 ,因此请使用==代替!=

if ($row['rest'] == ""  || $row['rest'] == "Select An Option") {
^--------------------^     ^--------------------------------^
if field 'rest' blank        if field is 'Select An Option'

This can be be improved by using empty() to perform the "is empty" check: 这可以通过使用empty()来执行“ is empty”检查来改进:

if (empty($row['test']) || $row['rest'] == "Select An Option") {

"If the field 'rest' is blank ...." - if that's true, then your logic is backwards: “如果'rest'字段为空....“-如果为真,则您的逻辑倒退:

if ($row['rest'] == '') || ($row['rest'] == 'Select an option') {
    $rest = 'rest stops';
} else {
    $rest = '';
}

Note the use of == for equality, rather than != for inequality. 请注意, ==用于相等,而不是!=用于不相等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM