检查PHP中的多个条件'if'声明


Checking multiple conditions in PHP 'if' statement

if($this->request->get['product_id'] == (53 || 52 || 43)){
    if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/product/product2.tpl')) {
        $this->template = $this->config->get('config_template') . '/template/product/product2.tpl';
    } else {
        $this->template = 'default/template/product/product2.tpl';
    }
} else{
    if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/product/product.tpl')) {
        $this->template = $this->config->get('config_template') . '/template/product/product.tpl';
    } else {
        $this->template = 'default/template/product/product.tpl';
    }
}

我想实现它,如果产品id是535043然后…同样的事情,但我不确定这样是正确的

您可以将产品id存储在数组中,然后使用in_array()函数:

if (in_array($this->request->get['product_id'], array (53, 52, 43))) {

in_array -检查一个值是否存在于数组

if($this->request->get['product_id'] == (53 || 52 || 43)){
应:

if($this->request->get['product_id'] == 53
   || $this->request->get['product_id'] == 52
   || $this->request->get['product_id'] == 43) {

您也可以使用in_array,这将使您的代码看起来更干净,但可能会更慢。Tim Cooper给出了示例代码

$prodId = $this->request->get['product_id'];
if($prodId == 53 || $prodId == 52 || $prodId == 43){
....