请推荐一种智能,优雅的方式来编写这些重复的“if”语句

时间:2010-07-16 16:50:39

标签: php if-statement formatting

我只是知道我这样做的方式非常糟糕:

<?php } elseif ( is_single()) { 

    $url = wp_get_referer();
    $path_parts = pathinfo($url);
    $mycat = $path_parts['filename'];

    if ( $mycat == "animation"  ) {
        $_SESSION["theCategory"] = $mycat;?>
        <a href="<?php bloginfo('home')?>/category/<?php echo $_SESSION["theCategory"];?>"><?php echo $_SESSION["theCategory"];?></a>
<?php } elseif ($mycat == "direction"){     
        $_SESSION["theCategory"] = $mycat;?>
        <a href="<?php bloginfo('home')?>/category/<?php echo $_SESSION["theCategory"];?>"><?php echo $_SESSION["theCategory"];?></a>
<?php } elseif ($mycat == "grading"){   
        $_SESSION["theCategory"] = $mycat;?>
        <a href="<?php bloginfo('home')?>/category/<?php echo $_SESSION["theCategory"];?>"><?php echo $_SESSION["theCategory"];?></a>
<?php } elseif ($mycat == "online"){    
        $_SESSION["theCategory"] = $mycat;?>
        <a href="<?php bloginfo('home')?>/category/<?php echo $_SESSION["theCategory"];?>"><?php echo $_SESSION["theCategory"];?></a>
<?php } elseif ($mycat == "showcase"){  
        $_SESSION["theCategory"] = $mycat;?>
        <a href="<?php bloginfo('home')?>/category/<?php echo $_SESSION["theCategory"];?>"><?php echo $_SESSION["theCategory"];?></a>
<?php } elseif ($mycat == "showreel"){  
        $_SESSION["theCategory"] = $mycat;?>
        <a href="<?php bloginfo('home')?>/category/<?php echo $_SESSION["theCategory"];?>"><?php echo $_SESSION["theCategory"];?></a>
<?php } elseif ($mycat == "vfx"){   
        $_SESSION["theCategory"] = $mycat;?>
        <a href="<?php bloginfo('home')?>/category/<?php echo $_SESSION["theCategory"];?>"><?php echo $_SESSION["theCategory"];?></a>
<?php }else{ ?><a href="<?php bloginfo('home')?>/category/<?php echo $_SESSION["theCategory"];?>"><?php echo $_SESSION["theCategory"];?></a><?php };?>

<?php } //end if is_single ?>

有效。但它是混乱和重复的。我尝试过这条路:

<?php if ($mycat == "animation" || "direction" || "grading"){

但它根本不起作用。

6 个答案:

答案 0 :(得分:7)

创建一个包含值的数组,然后测试数组中是否存在$mycat的值。此外,无论$mycat是什么,您都会回显链接,因此可以完全删除else语句,并将if结构进一步简化为以下内容:

$categories = array(
    'animation', 
    'direction', 
    'grading', 
    'online', 
    'showcase', 
    'showreel', 
    'vfx'
);

if (in_array($mycat, $categories)) {
    $_SESSION["theCategory"] = $mycat;
}

?>
<a href="<?php bloginfo('home')?>/category/<?php echo $_SESSION["theCategory"];?>"><?php echo $_SESSION["theCategory"];?></a>

和其他人一样,记得要逃避你的输出。

答案 1 :(得分:5)

这样的东西?

<?php
$cats = array(
    'animation',
    'direction',
    'grading',
    'online',
    'showcase',
    'showreel'
    'vfx',
);
if ( in_array($mycat, $cats) ) :
    $_SESSION['theCategory'] = $mycat; ?>
    <a href="<?php bloginfo('home')?>/category/<?php echo $mycat;?>"><?php echo $mycat;?></a>
<?php else: ?>
    <a href="<?php bloginfo('home')?>/category/<?php echo $_SESSION["theCategory"];?>"><?php echo $_SESSION["theCategory"];?></a>
<?php endif; ?>

答案 2 :(得分:4)

并使用switch声明:

switch($mycat) {
    case 'animation':
    case 'direction':
    case 'grading':
    case 'online':
    case 'showcase':
    case 'showreel':
    case 'vfx':
        $_SESSION["theCategory"] = $mycat; 
    default:
        ?>
        <a href="<?php bloginfo('home')?>/category/<?php echo $_SESSION["theCategory"];?>"><?php echo $_SESSION["theCategory"];?></a>
        <?php
}

请注意案例陈述后的落实情况。你可以这样做,或者:

switch($mycat) {
    case 'animation':
    case 'direction':
    case 'grading':
    case 'online':
    case 'showcase':
    case 'showreel':
    case 'vfx':
        $_SESSION["theCategory"] = $mycat; 
}
?>
<a href="<?php bloginfo('home')?>/category/<?php echo $_SESSION["theCategory"];?>"><?php echo $_SESSION["theCategory"];?></a>
<?php

答案 3 :(得分:2)

我不明白为什么你甚至需要在这里有条件。你并没有在每个条件可能性中改变任何东西。

$_SESSION["theCategory"] = $mycat;
<a href="<?php bloginfo('home')?>/category/<?php echo $_SESSION["theCategory"];?>"><?php echo $_SESSION["theCategory"];?></a>

答案 4 :(得分:1)

使用in_array()函数为您写条件:

<?php if (in_array($mycat, array("animation", "direction", "grading")))

答案 5 :(得分:1)

路径1:

<?php if ($mycat == "animation" || $mycat == "direction" || $mycat == "grading"){ 

路径2:

<?php if (in_array($mycat,array("animation","direction","grading"))) {