Php 整理if和elseif语句

Php 整理if和elseif语句,php,html,Php,Html,我已经用各种if和elseif语句拼凑了这段代码,只是想知道它是否可以整理一下(我的语法知识是垃圾!) 有没有一种方法可以将所有的elseif和if合并成一个,而不是再次显示所有的html代码(因为它是相同的) if(in_array("Branding", get_field('categories')) && $grid_title == "Branding"){ echo " <div class=\"grid-box\" onclick=\

我已经用各种if和elseif语句拼凑了这段代码,只是想知道它是否可以整理一下(我的语法知识是垃圾!)

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

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";
}
if(在数组中(“品牌化”,获取字段(“类别”)&&&$grid\u title==“品牌化”){
回声“
“;\”样式=\“光标:指针;\”>
拇指图像“\”alt=\”.$字段->公司名称“\”高度=\”152\”宽度=\“210\”/>
“$fields->company\u name”
.infrade(“,”,get_字段('categories'))
“$fields->project\u name”
“$fields->description。”

“$fields->company\u name” .infrade(“,”,get_字段('categories')) "; } elseif(在数组(“Web”,获取字段(“类别”)&&&$grid\U title==“Web”){ 回声“ “;\”样式=\“光标:指针;\”> 拇指图像“\”alt=\”.$字段->公司名称“\”高度=\”152\”宽度=\“210\”/> “$fields->company\u name” .infrade(“,”,get_字段('categories')) “$fields->project\u name” “$fields->description。”

“$fields->company\u name” .infrade(“,”,get_字段('categories')) "; } 否则{ 回音“你好”; }
elseif的功能与第一个if相同。因此,使用或将条件移动到第一个条件,并删除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";
}
if((在数组(“品牌”,获取字段(“类别”)中)和&$grid\u title==“品牌”);(在数组(“Web”,获取字段(“类别”)中)和&$grid\u title==“Web”)){
回声“
“;\”样式=\“光标:指针;\”>
拇指图像“\”alt=\”.$字段->公司名称“\”高度=\”152\”宽度=\“210\”/>
“$fields->company\u name”
.infrade(“,”,get_字段('categories'))
“$fields->project\u name”
“$fields->description。”

“$fields->company\u name” .infrade(“,”,get_字段('categories')) "; } 否则{ 回音“你好”; }
elseif的功能与第一个if相同。因此,使用或将条件移动到第一个条件,并删除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";
}
if((在数组(“品牌”,获取字段(“类别”)中)和&$grid\u title==“品牌”);(在数组(“Web”,获取字段(“类别”)中)和&$grid\u title==“Web”)){
回声“
“;\”样式=\“光标:指针;\”>
拇指图像“\”alt=\”.$字段->公司名称“\”高度=\”152\”宽度=\“210\”/>
“$fields->company\u name”
.infrade(“,”,get_字段('categories'))
“$fields->project\u name”
“$fields->description。”

“$fields->company\u name” .infrade(“,”,get_字段('categories')) "; } 否则{ 回音“你好”; }
< /代码> 您应该考虑使用PHP来分隔字符串。这将有助于消除回音和所有转义字符

使用PHP的简写语法。它使阅读更容易:

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

你应该考虑使用PHP来定义字符串。这将有助于消除回音和所有转义字符

使用PHP的简写语法。它使阅读更容易:

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

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

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

//html
//html

这使得阅读IMO更加容易。

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

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

//html
//html

这使得阅读IMO更容易。

这应该在代码审查上。两个第一选项之间有什么不同(incontent)吗?两个选项的内容相同,但if略有不同。这应该在代码审查上。有什么不同(incontent)在前两个选项之间?两个选项的内容相同,但if的内容略有不同。使用速记语法有什么问题?我个人觉得更容易阅读使用速记语法有什么问题?我个人觉得它更容易阅读