gpt4 book ai didi

php - "elseif"和 "else if"完全是同义词吗?

转载 作者:IT老高 更新时间:2023-10-28 11:43:30 25 4
gpt4 key购买 nike

elseifelse if 完全是同义词,还是有区别?

Zend 有公认的“标准”吗?

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

最佳答案

来自PHP manual :

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.

本质上,它们的行为是相同的,但 else if 在技术上等同于这样的嵌套结构:

if (first_condition)
{

}
else
{
if (second_condition)
{

}
}

手册还注明:

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.

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

if (first_condition)
{

}
elseif (second_condition)
{

}

可以使用 elseifelse if。但是,如果您使用 alternate syntax , 你必须使用 elseif:

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

关于php - "elseif"和 "else if"完全是同义词吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3662412/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com