整理if和elseif语句

时间:2011-05-19 11:20:56

标签: php html

我已经使用各种if和elseif语句将这段代码整合在一起,只是想知道它是否可以整理(我的语法知识是垃圾!):

而不是再次显示所有的html代码(因为它是相同的)是否有一种方法可以将所有elseif和if组合成一个?

if(in_array("Branding", get_field('categories')) && $grid_title == "Branding"){
     echo "
        <div class=\"grid-box\" onclick=\"location.href='" . get_page_link($post->ID) ."';\" style=\"cursor: pointer;\">
        <div class=\"phase-1\">
           <img class=\"grid-image\" src=\"" . $fields->thumb_image . "\" alt=\"" . $fields->company_name ."\" height=\"152\" width=\"210\" />
           <div class=\"grid-heading\">
                <h2>". $fields->company_name ."</h2>
                <h3>" . implode(', ',get_field('categories')) ."</h3>
            </div> 
        </div>
        <div class=\"phase-2\">
            <div class=\"grid-info\">
                <h4>". $fields->project_name ."</h4>
                <p>". $fields->description ."</p>
            </div>
            <div class=\"grid-heading-hover\">
                <h2>". $fields->company_name ."</h2>
                <h3>". implode(', ',get_field('categories')) ."</h3>
            </div> 
        </div>
    </div>
     ";
}
elseif(in_array("Web", get_field('categories')) && $grid_title == "Web"){
     echo "
        <div class=\"grid-box\" onclick=\"location.href='" . get_page_link($post->ID) ."';\" style=\"cursor: pointer;\">
        <div class=\"phase-1\">
           <img class=\"grid-image\" src=\"" . $fields->thumb_image . "\" alt=\"" . $fields->company_name ."\" height=\"152\" width=\"210\" />
           <div class=\"grid-heading\">
                <h2>". $fields->company_name ."</h2>
                <h3>" . implode(', ',get_field('categories')) ."</h3>
            </div> 
        </div>
        <div class=\"phase-2\">
            <div class=\"grid-info\">
                <h4>". $fields->project_name ."</h4>
                <p>". $fields->description ."</p>
            </div>
            <div class=\"grid-heading-hover\">
                <h2>". $fields->company_name ."</h2>
                <h3>". implode(', ',get_field('categories')) ."</h3>
            </div> 
        </div>
    </div>
     ";

}
else {
    echo "hello";
}

3 个答案:

答案 0 :(得分:4)

您应该考虑使用PHP的Heredoc来分隔字符串。这将有助于摆脱回声和所有逃脱的'\'字符。

使用PHP的if/else/elseif/endif简写语法。它使阅读更容易:

if(condition) :
  //statments
elseif(condition) :
  //statments   
endif;

答案 1 :(得分:1)

elseif与第一个if相同。因此,使用OR将条件移动到第一个条件并删除elseif:

if((in_array("Branding", get_field('categories')) && $grid_title == "Branding") || (in_array("Web", get_field('categories')) && $grid_title == "Web")){
     echo "
        <div class=\"grid-box\" onclick=\"location.href='" . get_page_link($post->ID) ."';\" style=\"cursor: pointer;\">
        <div class=\"phase-1\">
           <img class=\"grid-image\" src=\"" . $fields->thumb_image . "\" alt=\"" . $fields->company_name ."\" height=\"152\" width=\"210\" />
           <div class=\"grid-heading\">
                <h2>". $fields->company_name ."</h2>
                <h3>" . implode(', ',get_field('categories')) ."</h3>
            </div> 
        </div>
        <div class=\"phase-2\">
            <div class=\"grid-info\">
                <h4>". $fields->project_name ."</h4>
                <p>". $fields->description ."</p>
            </div>
            <div class=\"grid-heading-hover\">
                <h2>". $fields->company_name ."</h2>
                <h3>". implode(', ',get_field('categories')) ."</h3>
            </div> 
        </div>
    </div>
     ";
}
else {
    echo "hello";
}

答案 2 :(得分:1)

如果我是你,我会将HTML保留为纯文本,而不是PHP字符串:

<?php if(condition) : ?>
  // html
<?php elseif(condition) : ?>
  // html
<?php endif; ?>

这使得阅读IMO变得更加容易。