if / ifelse逻辑的效率

时间:2011-06-23 10:07:42

标签: php

我想知道是否有更有效的方法来编写以下内容,因为第一组URL都具有相同的结果。提前谢谢。

    <?php 
if (stripos($_SERVER['REQUEST_URI'],'/feet-foxes/') !== false) {
    echo '';
}
elseif (stripos($_SERVER['REQUEST_URI'],'/aerial-immersion/') !== false) {
    echo '';
}
elseif (stripos($_SERVER['REQUEST_URI'],'/taste-paper-air/') !== false) {
    echo '';
}
elseif (stripos($_SERVER['REQUEST_URI'],'/dance-movement-therapy-group/') !== false) {
    echo '';
}
elseif (stripos($_SERVER['REQUEST_URI'],'/fencing-fun/') !== false) {
    echo '';
}
elseif ($ticketlink = get_post_meta($post->ID, 'Ticket-Link-1', true)) {
    echo '<div class="artist-buy-tickets-box"><a class="artist-buy-button" href="'.$ticketlink.'" title="Buy tickets" target="_blank">Buy Tickets</a>';
}
else { 
    echo '<h6>TICKETS ON SALE<br/>JUNE 15 2011</h6>';
}
?>

6 个答案:

答案 0 :(得分:3)

我很确定你的效率标准是什么,但这是一个更好的选择。对于匿名函数,它需要PHP&gt; = 5.3(可以在早期版本中完成,但使用create_function是一件苦差事。)

$urlPaths = array(
    '/fencing-fun/',
    '/dance-movement-therapy-group/',
    '/taste-paper-air/',
    '/aerial-immersion/',
    '/feet-foxes/',
);

if (!empty(array_filter($urlPaths, 
                        function($item) {
                            return stripos($_SERVER['REQUEST_URI'], $item) !== false;
                        }))
{
    echo '';
}

我为残忍的缩进道歉 - 我无法想出任何不那么难看的东西。

答案 1 :(得分:1)

stripos()的调用很可能比调用get_post_meta()更便宜,所以首先要这样做。为了(微观)优化,让具有相同结果的五个测试成为单个表达式:

<?php 
if (stripos($_SERVER['REQUEST_URI'],'/feet-foxes/') !== false
        || stripos($_SERVER['REQUEST_URI'],'/aerial-immersion/') !== false
        || stripos($_SERVER['REQUEST_URI'],'/taste-paper-air/') !== false
        || stripos($_SERVER['REQUEST_URI'],'/dance-movement-therapy-group/') !== false
        || stripos($_SERVER['REQUEST_URI'],'/fencing-fun/') !== false) {
    echo '';
} elseif ($ticketlink = get_post_meta($post->ID, 'Ticket-Link-1', true)) {
    echo '<div class="artist-buy-tickets-box"><a class="artist-buy-button" href="'.$ticketlink.'" title="Buy tickets" target="_blank">Buy Tickets</a>';
} else { 
    echo '<h6>TICKETS ON SALE<br/>JUNE 15 2011</h6>';
}
?>

这消除了对重复代码(echo '';)的需要。您可以对测试进行排序,以便将最常用的URI放在首位,以便利用short-circuit evaluation(我怀疑这是严格的理论)。

答案 2 :(得分:1)

$needles = array('/feet-foxes/', '/aerial-immersion/', 'etc');
$found = false;
foreach($needles as $needle){
    if (stripos($_SERVER['REQUEST_URI'], $needle) !== false){
        $found = true;
        break;
    }
}
if(!$found){
    if ($ticketlink = get_post_meta($post->ID, 'Ticket-Link-1', true)) {
        echo '<div class="artist-buy-tickets-box"><a class="artist-buy-button"   href="'.$ticketlink.'" title="Buy tickets" target="_blank">Buy Tickets</a>';
    }
    else { 
        echo '<h6>TICKETS ON SALE<br/>JUNE 15 2011</h6>';
    }
}

您确定要在if?

中分配内容吗?

答案 3 :(得分:1)

重写如下。通过这种方式,可以轻松地向组中添加更多具有相同结果的URL。

$echo = null;

$paths = array(
    'feet-foxes',
    'aerial-immersion',
    'taste-paper-air',
    'dance-movement-therapy-group',
    'fencing-fun'
);

foreach ( $paths as $path ) {
    if ( stripos($_SERVER['REQUEST_URI'], '/'.$path.'/') !== false ) {
        $echo = '';
        break;
    }
}

if ( !is_null($echo) ) {
    echo '';
} else if ( $ticketlink = get_post_meta($post->ID, 'Ticket-Link-1', true) ) {
    echo '<div class="artist-buy-tickets-box"><a class="artist-buy-button" href="'.$ticketlink.'" title="Buy tickets" target="_blank">Buy Tickets</a>';
} else { 
    echo '<h6>TICKETS ON SALE<br/>JUNE 15 2011</h6>';
}

答案 4 :(得分:1)

if (!preg_match('#/(feet-foxes|aerial-immersion|taste-paper-air|dance-movement-therapy-group|fencing-fun)/#', $_SERVER['REQUEST_URI'])) {
    if ($ticketlink = get_post_meta($post->ID, 'Ticket-Link-1', true)) {
        echo '<div class="artist-buy-tickets-box"><a class="artist-buy-button" href="'.$ticketlink.'" title="Buy tickets" target="_blank">Buy Tickets</a>';
    }
    else { 
        echo '<h6>TICKETS ON SALE<br/>JUNE 15 2011</h6>';
    }
}

答案 5 :(得分:0)

如果采取相同的行动,只需将它们全部归入一个条件。但是,看起来你什么都不做,所以不应该要求顶部?

<?php 
if (stripos($_SERVER['REQUEST_URI'],'/feet-foxes/') !== false || 
    stripos($_SERVER['REQUEST_URI'],'/aerial-immersion/') !== false ||
    stripos($_SERVER['REQUEST_URI'],'/taste-paper-air/') !== false ||
    stripos($_SERVER['REQUEST_URI'],'/dance-movement-therapy-group/') !== false ||
    stripos($_SERVER['REQUEST_URI'],'/fencing-fun/') !== false
    ) {
    // If you're actually doing nothing - this whole section isn't needed?
    echo '';
}
elseif ($ticketlink = get_post_meta($post->ID, 'Ticket-Link-1', true)) {
    echo '<div class="artist-buy-tickets-box"><a class="artist-buy-button" href="'.$ticketlink.'" title="Buy tickets" target="_blank">Buy Tickets</a>';
}
else { 
    echo '<h6>TICKETS ON SALE<br/>JUNE 15 2011</h6>';
}
?>