PHP 中的分号是可选的吗? [英] Are semicolons in PHP optional?
问题描述
我知道 PHP 就像 Java 一样,而 C++ 使用分号来终止语句,所以我想知道如何将 PHP 与 HTML 内联使用.我想知道为什么省略分号有效.
I know that PHP, just like Java, and C++ use semicolons to terminate statements, so I'm wondering about using PHP inline with HTML. I am wondering why omitting semicolons works.
例如,为什么下面的代码有效?
For example, why does the following code work?
<?php if(true): ?>
<p>Hello World !!!</p>
<?php endif ?>
注意:
推荐答案
一般来说,我总是建议包含分号,但你是对的,在这种情况下可以删除分号.
In general, I would always recommend including the semi-colon, but you're right, it can be dropped in this instance.
当语句后紧跟 PHP 结束标记时,您只能在语句后删除分号——即 ?>
.
You may only drop the semi-colon after a statement when the statement is followed immediately by a closing PHP tag -- ie ?>
.
此处的 PHP 文档中对此进行了说明:http://php.net/manual/en/language.basic-syntax.instruction-separation.php
This is documented in the PHP documentation here: http://php.net/manual/en/language.basic-syntax.instruction-separation.php
这是该语言早期的一项功能,旨在使模板化代码更易于阅读.
This is a feature from the earliest days of the language, aimed at making templated code slightly easier to read.
与 Javascript 不同,在其他情况下不允许删除分号.
Unlike Javascript, there are no other circumstances where dropping the semi-colon is permitted.
这篇关于PHP 中的分号是可选的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!