php “elseif”和“else if”完全是同义词吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3662412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:36:38  来源:igfitidea点击:

Are "elseif" and "else if" completely synonymous?

phpsyntaxconditional-statements

提问by Theodore R. Smith

Are elseifand else ifcompletely synonymous, or is there a difference?

elseifelse if完全的代名词,或者是有区别吗?

Does Zend have an accepted "standard" on which one to use?

Zend 是否有一个公认的“标准”来使用?

While I personally dislike seeing elseifin the code, I just need to know if they're synonymous and the PHP manual isn't the easiest to search.

虽然我个人不喜欢elseif在代码中看到,但我只需要知道它们是否是同义词,并且 PHP 手册不是最容易搜索的。

回答by Daniel Vandersluis

From the PHP manual:

PHP 手册

In PHP, you can also write 'else if' (in two words) and the behavior would be identical to the one of 'elseif' (in a single word). The syntactic meaning is slightly different (if you're familiar with C, this is the same behavior) but the bottom line is that both would result in exactly the same behavior.

在 PHP 中,您还可以编写 'else if'(两个词),其行为与 'elseif'(一个词)相同。句法含义略有不同(如果您熟悉 C,这是相同的行为)但最重要的是,两者都会导致完全相同的行为。

Essentially, they will behave the same, but else ifis technically equivalent to a nested structure like so:

本质上,它们的行为相同,但else if在技​​术上等同于嵌套结构,如下所示:

if (first_condition)
{

}
else
{
  if (second_condition)
  {

  }
}

The manual also notes:

该手册还指出:

Note that elseif and else if will only be considered exactly the same when using curly brackets as in the above example. When using a colon to define your if/elseif conditions, you must not separate else if into two words, or PHP will fail with a parse error.

请注意,elseif 和 else if 只有在使用大括号时才会被视为完全相同,如上例所示。使用冒号定义 if/elseif 条件时,不得将 else if 分隔为两个词,否则 PHP 将因解析错误而失败。

Which means that in the normal control structure form (ie. using braces):

这意味着在正常的控制结构形式中(即使用大括号):

if (first_condition)
{

}
elseif (second_condition)
{

}

either elseifor else ifcan be used. However, if you use the alternate syntax, you must use elseif:

任一elseifelse if都可以使用。但是,如果您使用替代语法,则必须使用elseif

if (first_condition):
  // ...
elseif (second_condition):
  // ...
endif;

回答by James Cowhen

The Framework Interoperability Group (FIG) which is made up of members that include the developers of Zend ( https://github.com/php-fig/fig-standards#voting-members) , put together a series of Standard recommendations (PSR-#).

框架互操作性组 (FIG) 由包括 Zend 开发人员 ( https://github.com/php-fig/fig-standards#voting-members)的成员组成,汇集了一系列标准建议 (PSR -#)。

Zend2 and Symfony2 already follows PSR-0.

Zend2 和 Symfony2 已经遵循 PSR-0。

There's no hard and fast rules for styles, but you can try and follow as much of PSR-2 as you can.

样式没有硬性规定,但您可以尝试尽可能多地遵循 PSR-2。

There's a comment on else if vs elseif in PSR-2:

在 PSR-2 中有关于 else if 与 elseif 的评论:

The keyword elseif SHOULD be used instead of else if so that all control keywords look like single words.

应该使用关键字 elseif 而不是 else if ,以便所有控制关键字看起来都像单个单词。

https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md#51-if-elseif-else

https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md#51-if-elseif-else

Some of the recommendations are just that, recommendations. It's up to you whether to use else if or elseif

一些建议就是这样,建议。是否使用 else if 或 elseif 由您决定