更好地重写这些IF条款的方法?

时间:2015-04-04 11:13:07

标签: php if-statement

有没有办法有效地重写这个? 谢谢

<?php if ($row['website']) { ?>
    <li><a target="_blank" href="http://www.<?= $row['website']; ?>"><i class="icon icon_desktop"></i></a></li>
<?php } ?>
<?php if ($row['twitter']) { ?>
    <li><a target="_blank" href="http://www.twitter.com/<?= $row['twitter']; ?>"><i class="icon social_twitter"></i></a></li>
<?php } ?>                      
<?php if ($row['facebook']) { ?>
    <li><a target="_blank" href="http://www.facebook.com/<?= $row['facebook']; ?>"><i class="icon social_facebook"></i></a></li>
<?php } ?>
<?php if ($row['linkedin']) { ?>
    <li><a target="_blank" href="http://www.linkedin.com/in/<?= $row['linkedin']; ?>"><i class="icon social_linkedin"></i></a></li>
<?php } ?>
<?php if ($row['youtube']) { ?>
    <li><a target="_blank" href="http://www.youtube.com/user/<?= $row['youtube']; ?>"><i class="icon social_youtube"></i></a></li>
<?php } ?>

2 个答案:

答案 0 :(得分:3)

我觉得这样的事情对你有用:

(这里我只是将所有键放入一个数组中,然后我用array_filter()进行过滤。之后你可以简单地循环遍历所有过滤的键)

<?php

    $keys = ["http://www." => "website", "http://www.twitter.com/" => "twitter", "http://www.facebook.com/" => "facebook", "http://www.linkedin.com/in/"=> "linkedin", "http://www.youtube.com/user/"=> "youtube"];
    $keys = array_filter($keys, function($v)use($row){return $row[$v];});

    foreach($keys as $k => $key)
        echo "<li><a target='_blank' href='" . $k . $row[$key] . "'><i class='icon icon_" . $key . "'></i></a></li>";

?>

(适用额外)编辑:

对于php版本低于&lt; 5.4(echo phpversion();)的人:
因为我们住在2015年,get the current version of PHP right here right now!

$keys = array("http://www." => "website", "http://www.twitter.com/" => "twitter", "http://www.facebook.com/" => "facebook", "http://www.linkedin.com/in/"=> "linkedin", "http://www.youtube.com/user/"=> "youtube");
function filter($v){global $row; return $row[$v];}
$keys = array_filter($keys, "filter");

foreach($keys as $k => $key)
    echo "<li><a target='_blank' href='" . $k . $row[$key] . "'><i class='icon icon_" . $key . "'></i></a></li>";

示例输出:

<li><a target='_blank' href='http://www.twitter.com/a'><i class='icon icon_twitter'></i></a></li>
<li><a target='_blank' href='http://www.linkedin.com/b'><i class='icon icon_linkedin'></i></a></li>
<li><a target='_blank' href='http://www.youtube.com/c'><i class='icon icon_youtube'></i></a></li>

答案 1 :(得分:1)

当然不会更短,但更容易在我眼中阅读和维护:

<?php 
foreach (array('website', 'twitter', 'facebook', 'linkedin', 'youtube') as $site) { 
    if ($row[$site]) {
        switch($site) {
            case 'website':  
                $urlBase = 'http://www.';
                $icon = 'icon_desktop'; 
                break;
            case 'twitter':  
                $urlBase = 'http://www.twitter.com/';  
                $icon = 'social_twitter'; 
                break;
            case 'facebook': 
                $urlBase = 'http://www.facebook.com/'; 
                $icon = 'social_facebook'; 
                break;
            case 'linkedin': 
                $urlBase = 'http://www.linkedin.com/in/'; 
                $icon = 'social_linkedin'; 
                break;
            case 'youtube':  
                $urlBase = 'http://www.youtube.com/user/';  
                $icon = 'social_youtube'; 
                break;
        }
        echo sprintf('<li><a target="_blank" href="%s%s"><i class="icon %s"></i></a></li>'."\n", $urlBase, $row[$site], $icon);
    }
}
?>

在健壮性和可维护性方面,通常显式优于隐式。