簡體   English   中英

整理if和elseif語句

[英]Tidy up if and elseif statement

我將這些代碼與各種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";
}

您應該考慮使用PHP的Heredoc來分隔字符串。 這將有助於擺脫回聲和所有轉義的“ \\”字符。

使用PHP的if / else / elseif / endif簡寫語法。 它使閱讀更容易:

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

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";
}

如果我是你,我會將HTML保留為純文本,而不是PHP字符串:

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

這樣可以更輕松地閱讀IMO。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM