Php:while...endwhile 有什么区别;而 { // 这里的东西 }

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4650971/
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 13:51:45  来源:igfitidea点击:

Php: what's the difference between while...endwhile; and while { // stuff here }

php

提问by redconservatory

What's the difference between

有什么区别

while (expression):

// do stuff

endwhile;

and

while {

}

回答by Jeff Davis

There is no functional difference.

没有功能差异。

In practical use I find that:

在实际使用中,我发现:

while (expression):
// do stuff
endwhile;

Is more readable for the designers when you are embedding php code within html. IE:

当您在 html 中嵌入 php 代码时,对设计人员来说更具可读性。IE:

<? while ($cssClass = array_pop($array)): ?>
   <li class="<?=$cssClass?>">
<? endwhile; ?>

Whereas:

然而:

while {

}

Is more readable within a php code block.

在 php 代码块中更具可读性。

回答by S M

There's no difference, it comes down to personal preference.

没什么区别,看个人喜好。

回答by Jon

There's no real difference when writing code.

编写代码时没有真正的区别。

There can be a difference in levels of convenience in very special circumstances. For example, suppose you are writing a template engine that converts template code to native PHP code which is then cached and executed directly for speed.

在非常特殊的情况下,便利程度可能会有所不同。例如,假设您正在编写一个模板引擎,它将模板代码转换为原生 PHP 代码,然后缓存并直接执行以提高速度。

In this case, the fact that while...endwhile;avoids using braces may allow you to simplify your parsing algorithm if e.g. it recognizes variables that should be substituted with a syntax like {$var}, which also uses braces.

在这种情况下,如果while...endwhile;避免使用大括号的事实可能允许您简化解析算法,例如,如果它识别应该用类似语法替换的变量{$var},它也使用大括号。

Of course this is a pretty small benefit in a really extraordinary situation, but you take what you can. :)

当然,在非常特殊的情况下,这只是一个很小的好处,但你可以尽你所能。:)

回答by Brandon Lee

The difference is negligible when the code is actually run, but when coding I find that typing the brackets is (1): quicker, (2): more conventional, and (3): allows for less chance of error (endwhle anyone?).

当代码实际运行时,差异可以忽略不计,但在编码时我发现输入括号是(1):更快,(2):更传统,以及(3):允许更少的错误机会(结束任何人?) .

As a bonus, the editor I use auto-formats the while loops (with brackets, by default) and down the road, if anything is off, the built-in bracket-matching function will catch it.

作为奖励,我使用的编辑器会自动格式化 while 循环(默认情况下带括号),然后,如果有任何关闭,内置的括号匹配功能将捕获它。