如果条件如何简化?

时间:2012-01-31 03:04:03

标签: php

我如何简化条件,因为每个条件我创建一个新的if / elseif及其大量代码,这就是我所拥有的:

$chapters = array('1:data1', '2:data2', '4:datax', '3:datag');
sort($chapters);
$screenshots = array('1:screen1', '2:screen2', '3:screen3', '4:go4');
$chapterCount = count($chapters);
$chapterItems = 0;

foreach ($screenshots as $key => $screenshot) {
    $screenshotInfo = explode(':', $screenshot);
    $screen[$screenshotInfo[0]] = $screenshotInfo[1];
}

foreach ($chapters as $chapter) {
    $chapterInfo = explode(':', $chapter);
    $chapterNumber = current($chapterInfo);
    // If is the first chapter
    if ($chapterNumber == 1) {
        echo '<li>'.$chapterInfo[0].'</li>';
        // If have only one chapter
        if ($chapterItems+1 == $chapterCount) {
        echo '<li>'.$screen[$chapterNumber].'</li>';
        }
    }
    // If is a new chapter
    elseif ($currentNumber != $chapterNumber) {
        echo '<li>'.$screen[$chapterNumber-1].'</li>';
        echo '<li>'.$chapterInfo[0].'</li>';
        // If is new and last chapter
        if ($chapterItems+1 == $chapterCount) {
        echo '<li>'.$screen[$chapterNumber].'</li>';
        }
    }
    // If is the last chapter
    elseif ($chapterItems+1 == $chapterCount) {
        echo '<li>'.$chapterInfo[0].'</li>';
        echo '<li>'.$screen[$chapterNumber].'</li>';
    }
    else {
        echo '<li>'.$chapterInfo[0].'</li>';
    }

    $currentNumber = $chapterNumber;
    $chapterItems++;
}

该代码在我的测试中运行良好,但我确信它有很多不需要的代码。

2 个答案:

答案 0 :(得分:1)

如果简洁性比可读性更有意义,则可以使用ternary statements

$foo = true;
$bar = $foo ? 'something' : 'nothing';
echo $bar;
 //returns 'something'
$foo = false;
$bar = $foo ? 'something' : 'nothing';
$echo bar;
 //returns 'nothing'

答案 1 :(得分:1)

可能是你可以为所有if条件使用一个函数。由于内部的所有if条件都相同,我们可以使用类似的东西:

$chapters = array('1:data1', '2:data2', '4:datax', '3:datag');
sort($chapters);
$screenshots = array('1:screen1', '2:screen2', '3:screen3', '4:go4');
$chapterCount = count($chapters);
$chapterItems = 0;

foreach ($screenshots as $key => $screenshot) {
    $screenshotInfo = explode(':', $screenshot);
    $screen[$screenshotInfo[0]] = $screenshotInfo[1];
}

foreach ($chapters as $chapter) {
    $chapterInfo = explode(':', $chapter);
    $chapterNumber = current($chapterInfo);
    if ($chapterNumber == 1) { // If is the first chapter
        echo '<li>'.$chapterInfo[0].'</li>';
        echo compare($chapterItems,$chapterCount,$screen,$chapterNumber); // If have only one chapter
    } elseif ($currentNumber != $chapterNumber) { // If is a new chapter
        echo '<li>'.$screen[$chapterNumber-1].'</li>';
        echo '<li>'.$chapterInfo[0].'</li>';
        echo compare($chapterItems,$chapterCount,$screen,$chapterNumber); // If is new and last chapter
    } elseif ($chapterItems+1 == $chapterCount) { // If is the last chapter
        echo '<li>'.$chapterInfo[0].'</li>';
        echo '<li>'.$screen[$chapterNumber].'</li>';
    } else {
        echo '<li>'.$chapterInfo[0].'</li>';
    }

    $currentNumber = $chapterNumber;
    $chapterItems++;
}

function compare($chapterItems,$chapterCount,$screen,$chapterNumber) {

    if ($chapterItems+1 == $chapterCount) {
       return '<li>'.$screen[$chapterNumber].'</li>';
    } 

    return false;
}

希望这可以帮助你:)