php if () { } 和 if () 的区别: endif;
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/564130/
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
Difference between if () { } and if () : endif;
提问by alex
Are there any differences between...
之间有什么区别吗...
if ($value) {
}
...and...
...和...
if ($value):
endif;
?
?
回答by Thomaschaaf
They are the same but the second one is great if you have MVC in your code and don't want to have a lot of echos in your code. For example, in my .phtmlfiles (Zend Framework) I will write something like this:
它们是相同的,但是如果您的代码中有 MVC 并且不想在您的代码中有很多回声,则第二个很棒。例如,在我的.phtml文件(Zend 框架)中,我将编写如下内容:
<?php if($this->value): ?>
Hello
<?php elseif($this->asd): ?>
Your name is: <?= $this->name ?>
<?php else: ?>
You don't have a name.
<?php endif; ?>
回答by dprevite
I personally really hate the alternate syntax. One nice thing about the braces is that most IDEs, vim, etc all have bracket highlighting. In my text editor I can double click a brace and it will highlight the whole chunk so I can see where it ends and begins very easily.
我个人非常讨厌替代语法。关于大括号的一件好事是大多数 IDE、vim 等都有括号突出显示。在我的文本编辑器中,我可以双击一个大括号,它会突出显示整个块,这样我就可以很容易地看到它的结束和开始。
I don't know of a single editor that can highlight endif, endforeach, etc.
我不知道有哪个编辑器可以突出显示 endif、endforeach 等。
回答by dprevite
I think this say it all:
我认为这说明了一切:
this alternative syntax is excellent for improving legibility (for both PHP and HTML!) in situations where you have a mix of them.
这种替代语法非常适合在混合使用它们的情况下提高易读性(对于 PHP 和 HTML!)。
http://ca3.php.net/manual/en/control-structures.alternative-syntax.php
http://ca3.php.net/manual/en/control-structures.alternative-syntax.php
When mixing HTML an PHP the alternative sytnax is much easier to read. In normal PHP documents the traditional syntax should be used.
当混合 HTML 和 PHP 时,替代 sytnax 更容易阅读。在普通的 PHP 文档中,应该使用传统的语法。
回答by gahooa
At our company, the preferred way for handling HTML is:
在我们公司,处理 HTML 的首选方式是:
<? if($condition) { ?>
HTML content here
<? } else { ?>
Other HTML content here
<? } ?>
In the end, it really is a matter of choosing one and sticking with it.
归根结底,这真的是一个选择并坚持下去的问题。
回答by cregox
They are indeed both the same, functionally.
在功能上,它们确实是相同的。
But if the endifis getting too far from the correspondent ifI think it's much better practice to give a referencing comment to it. Just so you can easily find where it was open. No matter what language it is:
但是,如果endif离通讯员太远,if我认为给它一个参考评论是更好的做法。这样您就可以轻松找到它的打开位置。不管是什么语言:
if (my_horn_is_red or her_umbrella_is_yellow)
{
// ...
// let's pretend this is a lot of code in the middle
foreach (day in week) {
sing(a_different_song[day]);
}
// ...
} //if my_horn_is_red
That actually applies to any analogous "closing thing"! ;)
这实际上适用于任何类似的“关闭事物”!;)
Also, in general, editors deal better with curly brackets, in the sense they can point you to where it was open. But even that doesn't make the descriptive comments any less valid.
此外,一般来说,编辑器更好地处理大括号,因为它们可以将您指向它打开的位置。但即使这样也不会使描述性评论的有效性降低。
回答by Jeremy Ruten
Here's where you can find it in the official documentation: PHP: Alternative syntax for control structures
您可以在官方文档中找到它:PHP:控制结构的替代语法
回答by Harry Mustoe-Playfair
I think that it's particularly clearer when you're using a mix of ifs, fors and foreaches in view scripts:
我认为当您在视图脚本中混合使用ifs、fors 和foreaches时,它会特别清晰:
<?php if ( $this->hasIterable ): ?>
<h2>Iterable</h2>
<ul>
<?php foreach ( $this->iterable as $key => $val ):?>
<?php for ( $i = 0; $i <= $val; $i++ ): ?>
<li><?php echo $key ?></li>
<?php endfor; ?>
<?php endforeach; ?>
</ul>
<?php elseif ( $this->hasScalar ): ?>
<h2>Scalar</h2>
<?php for ( $i = 0; $i <= $this->scalar; $i++ ): ?>
<p>Foo = Bar</p>
<?php endfor; ?>
<?php else: ?>
<h2>Other</h2>
<?php if ( $this->otherVal === true ): ?>
<p>Spam</p>
<?php else: ?>
<p>Eggs</p>
<?php endif; ?>
<?php endif; ?>
as opposed to:
与:
<?php if ( $this->hasIterable ){ ?>
<h2>Iterable</h2>
<ul>
<?php foreach ( $this->iterable as $key => $val ){?>
<?php for ( $i = 0; $i <= $val; $i++ ){ ?>
<li><?php echo $key ?></li>
<?php } ?>
<?php } ?>
</ul>
<?php } elseif ( $this->hasScalar ){ ?>
<h2>Scalar</h2>
<?php for ( $i = 0; $i <= $this->scalar; $i++ ){ ?>
<p>Foo = Bar</p>
<?php } ?>
<?php } else { ?>
<h2>Other</h2>
<?php if ( $this->otherVal === true ){ ?>
<p>Spam</p>
<?php } else { ?>
<p>Eggs</p>
<?php } ?>
<?php } ?>
This is especially useful for long control statements where you might not be able to see the top declaration from the bottom brace.
这对于可能无法从底部大括号看到顶部声明的长控制语句特别有用。
回答by livefree75
I think that it really depends on your personal coding style. If you're used to C++, Javascript, etc., you might feel more comfortable using the {} syntax. If you're used to Visual Basic, you might want to use the if : endif; syntax.
我认为这真的取决于你的个人编码风格。如果您习惯了 C++、Javascript 等,您可能会觉得使用 {} 语法更舒服。如果你习惯了 Visual Basic,你可能想使用 if : endif; 句法。
I'm not sure one can definitively say one is easier to read than the other - it's personal preference. I usually do something like this:
我不确定有人能肯定地说一个比另一个更容易阅读——这是个人喜好。我通常做这样的事情:
<?php
if ($foo) { ?>
<p>Foo!</p><?php
} else { ?>
<p>Bar!</p><?php
} // if-else ($foo) ?>
Whether that's easier to read than:
是否比以下内容更容易阅读:
<?php
if ($foo): ?>
<p>Foo!</p><?php
else: ?>
<p>Bar!</p><?php
endif; ?>
is a matter of opinion. I can see why some would feel the 2nd way is easier - but only if you haven't been programming in Javascript and C++ all your life. :)
是见仁见智。我可以理解为什么有些人会觉得第二种方法更容易——但前提是你一生都没有用 Javascript 和 C++ 编程。:)
回答by Itay Moav -Malimovka
Both are the same.
两者都是一样的。
But: If you want to use PHP as your templating language in your view files(the V of MVC) you can use this alternate syntax to distinguish between php code written to implement business-logic (Controller and Model parts of MVC) and gui-logic. Of course it is not mandatory and you can use what ever syntax you like.
但是:如果您想在视图文件(MVC 的 V)中使用 PHP 作为模板语言,您可以使用这种替代语法来区分为实现业务逻辑而编写的 php 代码(MVC 的控制器和模型部分)和 gui-逻辑。当然,这不是强制性的,您可以使用任何您喜欢的语法。
ZF uses that approach.
ZF 使用这种方法。
回答by Rob Kennedy
There is no technical difference between the two syntaxes. The alternative syntax is not new; it was supported at least as far back as PHP 4, and perhaps even earlier.
两种语法之间没有技术差异。替代语法并不新鲜。至少早在 PHP 4 时就支持它,甚至更早。
You might prefer the alternative form because it explicitly states which control structure is ending: endwhile, for example, can only terminate a whileblock, whereas if you encounter a brace, it could be closing anything.
您可能更喜欢另一种形式,因为它明确说明了哪个控制结构正在结束:endwhile例如,只能终止一个while块,而如果遇到一个大括号,它可能会关闭任何东西。
You might prefer the traditional syntax, though, if you use an editor that has special support for braces in other C-like syntaxes. Vim, for example, supports several keystrokes for navigating to matching braces and to the starts and ends of brace-delimited blocks. The alternative syntax would break that editor feature.
但是,如果您使用的编辑器对其他类似 C 的语法中的大括号有特殊支持,则您可能更喜欢传统语法。例如,Vim 支持多次击键以导航到匹配的大括号以及大括号分隔块的开头和结尾。替代语法会破坏该编辑器功能。

