多个php if($category->;getId()==

多个php if($category->;getId()==,php,if-statement,Php,If Statement,我根据类别显示动态内容。 代码运行得非常好,除了我想在一个if语句中组合多个getId。目前每个类别Id都是根据if或elsif语句调用的。这给了我很多额外的代码。 是否可以在1 if语句中组合多个类别id 当前代码: <?php $category = Mage::getModel('catalog/layer')->getCurrentCategory();?> <?php if($category->getId()==1): ?> text 1 <

我根据类别显示动态内容。 代码运行得非常好,除了我想在一个if语句中组合多个getId。目前每个类别Id都是根据if或elsif语句调用的。这给了我很多额外的代码。 是否可以在1 if语句中组合多个类别id

当前代码:

<?php $category = Mage::getModel('catalog/layer')->getCurrentCategory();?> 
<?php if($category->getId()==1): ?> text 1
<?php elseif($category->getId()==2): ?> text 1
<?php elseif($category->getId()==3): ?> text 1
<?php else: ?> text 2
<?php endif; ?>

文本1
文本1
文本1
文本2

我通常通过使用函数来实现这一点。因此:

<?php
    $myValidIds = array(1,2,3);
    if(in_array($category->getId(), $myValidIds)){
        // Do something....
    }
?>


一个简单的in_数组就足够了

$allowed = array('1', '2', '3');
if (inarray($category->getId(), $allowed)){
    //it exists, show text 1
}else{
    //it doesnt exist, show text 2
}
为什么不呢

$cat = $category->getId();
if($cat==1) ...

如果在id等于1、2或3时提供“文本1”,为什么不能直接键入

<?php if($category->getId()==1 || $category->getId()==2 || $category->getId()==3): ?> text 1
text 1

请写一个没有代码的标题。这太难看了,不可读了……每次都会调用getId()方法!好吧,它可以工作。此外,作为一个J2EE程序员,我在这漂亮的100个符号行中找不到任何难看的东西;)好吧,作为一个PHP程序员,我发现它没有什么问题!很高兴我能帮忙。