简体   繁体   English

正确的If语句使用方法

[英]Correct way to use If statements

Need help understanding proper way of using "if elseif else" statements. 需要帮助来理解使用“ if elseif else”语句的正确方法。 CODE A below works but is it correct? 下面的代码A可行,但是正确吗? Am I allowed to end the statement with "elseif"? 我是否可以使用“ elseif”结束语句? Or are "if" statements supposed to end with either "endif" or "else" ? 还是“ if”语句以“ endif”或“ else”结尾?

CODE A: This code works 代码A:此代码有效

But notice it does not end with "endif" or "else". 但是请注意,它并不以“ 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'); } 
?>

.
Are all "if statements" supposed to end with "endif"? 是否所有“ if语句”都应以“ endif”结尾? If yes, can you show me the code? 如果是,您能告诉我代码吗? In CODE B below using "endif" results in White Screen error. 在下面的代码B中,使用“ endif”会导致白屏错误。

CODE B: endif does NOT work 代码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;
?>

.
Is the last "elseif" supposed to be "else" instead? 最后一个“ elseif”应该改为“ else”吗? If yes, can you show me the code? 如果是,您能告诉我代码吗? In CODE C below using "else" results in White Screen error. 在下面的CODE C中,使用“ else”会导致白屏错误。

CODE C: "else" instead of "elseif" does NOT work 代码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'); }
?>

The endif is only used with the "alternate syntax" http://php.net/manual/en/control-structures.alternative-syntax.php personally i wouldn't use it, but its up to you. endif仅与“备用语法”一起使用http://php.net/manual/zh-cn/control-structures.alternative-syntax.php我个人不会使用它,但取决于您。

per your "else" question .. it is optional. 根据您的“其他”问题..它是可选的。 its the "failsafe" ... if nothing else matches then it is executed. 它的“故障保护” ...如果没有其他匹配项,则执行它。 if you don't have an "else" and all of your elseif's fail, then nothing will be done in that code block. 如果您没有“ else”并且所有其他elseif失败,则在该代码块中将不执行任何操作。

You can make use of : and endif OR curly brackets ( {} ), but not both. 您可以使用:endif 大括号( {} ),但不能同时使用。 Either of these syntaxes will work 这些语法都可以使用

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

or 要么

if( something_is() ) {
  do something;
}

The second is actually the prefered way as it is easier to debug as nearly all code editors support this syntax. 第二种实际上是首选方法,因为几乎所有代码编辑器都支持此语法,因此调试起来更容易。 The first is quite hard to debug as it is not supported by most of the code editors 第一个很难调试,因为大多数代码编辑器都不支持它

people tend to use this syntax in php-based templates, because it kinda looks template-ish 人们倾向于在基于php的模板中使用此语法,因为它看起来有点像模板

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

as for backend stuff its recommended to use this syntax 至于后端的东西,建议使用这种语法

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

php also allows this PHP也允许这个

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

bottom line, you can only combine one type of syntax 最重要的是,您只能组合一种语法

I would say Code A. else is used when you want to say: 我想说代码A。 else ,当您想说:

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

But in your code you have a special case in the last condition. 但是在您的代码中,最后一种情况是特殊情况。 So CodeA. 所以CodeA。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM