首页 > 解决方案 > 具有多个条件的 PHP if 语句

问题描述

以下 if 语句的有效语法如何?

if ($properties->offering_type === 'y' || $properties->offering_type === 'p' && $properties->sold != 'y') { 
  // echo something
} else {
}

我想什么echo something时候offering_typey或者不是psoldy

标签: php

解决方案


&&的优先级高于||,因此您的条件被解释为

if ($properties->offering_type === 'y' || 
    ($properties->offering_type === 'p' && $properties->sold != 'y')) { 

您需要添加括号将它们组合||在一起。

if (($properties->offering_type === 'y' || $properties->offering_type === 'p')
    && $properties->sold != 'y') { 

推荐阅读