如果条件,简化PHP乘法

时间:2014-10-24 12:00:58

标签: php smarty prestashop

我想知道有没有办法让这段代码变得更加微妙?

 public function hookDisplayHeader()
{
    if(Tools::getValue('controller')=='index'){
        $this->context->controller->addCSS(($this->_path).'something.css', 'all');
    }
    if(Tools::getValue('controller')=='cms'){
        $this->context->controller->addCSS(($this->_path).'something.css', 'all');
    }
    if(Tools::getValue('controller')=='product'){
        $this->context->controller->addCSS(($this->_path).'something.css', 'all');
    }
    if(Tools::getValue('controller')=='category'){
        $this->context->controller->addCSS(($this->_path).'something.css', 'all');
    }

}

简单

 public function hookDisplayHeader()
     {
         if(Tools::getValue('controller')=='index AND product AND cms AND category'){
        $this->context->controller->addCSS(($this->_path).'something.css', 'all');
         }
     }

此代码不起作用:(

2 个答案:

答案 0 :(得分:2)

使用in_array();

public function hookDisplayHeader()
{
    $values = array('index','cms','product','category');
    if(in_array(Tools::getValue('controller'), $values)){
        $this->context->controller->addCSS(($this->_path).'something.css', 'all');
    }
}

答案 1 :(得分:1)

我会将它们放在一个数组中并检查:

$somethingCSSControllers = array('index','product','cms','category');

if(in_array(Tools::getValue('controller'),$somethingCSSControllers)){
    $this->context->controller->addCSS(($this->_path).'something.css', 'all');
}