【问题标题】:Correct way to use If statements正确使用 If 语句的方法
【发布时间】:2014-10-21 17:29:50
【问题描述】:

需要帮助理解使用“if elseif else”语句的正确方法。下面的代码 A 有效,但它正确吗?我可以用“elseif”结束陈述吗?还是“if”语句应该以“endif”或“else”结尾?

代码 A:此代码有效

但请注意,它不以“endif”或“else”结尾。

if ( is_page( 'contact' ) ) { get_template_part('inc/contact'); }
elseif ( is_page( 'policy' ) ) { get_template_part('inc/policy'); }
elseif ( is_page( 'about' ) ) { get_template_part('inc/about'); }
elseif ( is_page( 'terms' ) ) { get_template_part('inc/terms'); } 
?>

.
所有“if 语句”都应该以“endif”结尾吗?如果是的话,你能告诉我代码吗? 在下面的代码 B 中使用“endif”会导致白屏错误。

代码 B:endif 不起作用

if ( is_page( 'contact' ) ) { get_template_part('inc/contact'); }
elseif ( is_page( 'policy' ) ) { get_template_part('inc/policy'); }
elseif ( is_page( 'about' ) ) { get_template_part('inc/about'); }
elseif ( is_page( 'terms' ) ) { get_template_part('inc/terms'); } 
endif;
?>

.
最后一个“elseif”应该是“else”吗?如果是的话,你能告诉我代码吗? 在下面的 CODE C 中使用“else”会导致白屏错误。

代码 C:“else”而不是“elseif”不起作用

if ( is_page( 'contact' ) ) { get_template_part('inc/contact'); }
elseif ( is_page( 'policy' ) ) { get_template_part('inc/policy'); }
elseif ( is_page( 'about' ) ) { get_template_part('inc/about'); }
else ( is_page( 'terms' ) ) { get_template_part('inc/terms'); }
?>

【问题讨论】:

  • PHP - if。您不需要endif(括号{} 决定条件内的内容以及结束的时间)。 CODE C 不起作用的原因是因为else 是“如果这些都不是,那么就这样做”。如果你想要另一个条件,你需要使用elseif。在您的最后一个示例中,只需将else 切换为elseif。 (基本上,答案是 CODE A 是正确的做法。)。如果您使用过if (condition):,则使用endif(注意:)
  • 您是否考虑过为此使用开关?它会更容易阅读!
  • 您也可以使用数组将页面映射到路径,这样会更容易维护。
  • @joroen 你能告诉我如何将 CODE A 转换为“将页面映射到路径的数组”吗?我不知道该怎么做

标签: php wordpress if-statement


【解决方案1】:

endif 仅用于“替代语法”http://php.net/manual/en/control-structures.alternative-syntax.php 我个人不会使用它,但它取决于你。

根据您的“其他”问题.. 它是可选的。它的“故障安全”......如果没有其他匹配,那么它就会被执行。如果你没有“else”并且你所有的 elseif 都失败了,那么在那个代码块中什么都不会做。

【讨论】:

    【解决方案2】:

    您可以使用:endif OR 大括号 ({}),但不能同时使用。这些语法中的任何一个都可以工作

    if( something_is() ) :
      do something;
    endif;
    

    if( something_is() ) {
      do something;
    }
    

    第二种实际上是首选方式,因为它更容易调试,因为几乎所有代码编辑器都支持这种语法。第一个很难调试,因为大多数代码编辑器都不支持它

    【讨论】:

    • 感谢您演示这两种方法!我尝试勾选两个答案,但没有成功 =/ 顺便说一句,您知道如何将 CODE A 转换为 joroen 建议的“将页面映射到路径的数组”吗?
    • 欢迎您。是的,你只能接受一个答案。您接受的答案比我的信息要多得多:-)。享受
    【解决方案3】:

    人们倾向于在基于 php 的模板中使用这种语法,因为它看起来有点像模板

       <? if (something) : ?>
        <b>Yea!</b>
        <? else: ?>
        <p>nothing</p>
        <? endif; ?>
    

    至于后端的东西,建议使用这种语法

    if (something) { bar(); } 
    elseif (other)
    {
      yes();
    }
    ?>
    

    php 也允许这样做

    if (something) myThing() elseif (that) other(); else doNot();
    

    归根结底,你只能组合一种语法

    【讨论】:

    • if (something) myThing() elseif (that) other(); else doNot(); 不知道为什么会出现这种语法。调试是一场噩梦。虽然它是 100% 有效的 PHP,但我根本不使用它。花括号仍然是最好的
    • 完全同意,只是指出可以做什么
    【解决方案4】:

    我会说代码 A。 else 在你想说的时候使用:

    if(/*option;*/)
    {
    //code
    }
    elseif(/*another option;*/)
    {
    //code
    }else /*Any other options beside the above*/ {/*code;*/}
    

    但是在您的代码中,您在最后一个条件中有一个特殊情况。所以代码A。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多