PHP 8.3.0 RC 6 available for testing

指令分隔符

同 C 或 Perl 一样,PHP 需要在每个语句后用分号结束指令。一段 PHP 代码中的结束标记隐含表示了一个分号;在一个 PHP 代码段中的最后一行可以不用分号结束。如果后面还有新行,则代码段的结束标记包含了行结束。

示例 #1 包含末尾换行符的结束标记的例子

<?php echo "Some text"; ?>
No newline
<?= "But newline now" ?>

以上示例会输出:

 
Some textNo newline
But newline now

进入和退出 PHP 解析的例子:

<?php
echo 'This is a test';
?>

<?php echo 'This is a test' ?>

<?php echo 'We omitted the last closing tag';

注意:

文件末尾的 PHP 代码段结束标记可以不要,有些情况下当使用 include 或者 require 时省略掉会更好些,这样不期望的空白符就不会出现在文件末尾,之后仍然可以输出响应标头。在使用输出缓冲时也很便利,就不会看到由包含文件生成的不期望的空白符。

add a note

User Contributed Notes 3 notes

up
91
Krishna Srikanth
17 years ago
Do not mis interpret

<?php echo 'Ending tag excluded';

with

<?php echo 'Ending tag excluded';
<
p>But html is still visible</p>

The second one would give error. Exclude ?> if you no more html to write after the code.
up
17
M1001
1 year ago
You are also able to write more than one statement in one line, just separating with a semicolon, example:

<?php
echo "a"; echo "b"; echo "c";
#The output will be "abc" with no errors
?>
up
-38
anisgazig at gmail dot com
3 years ago
At the end of each statement,we will use a semicolon to seperate each statement.Php closing tag autometically implies a semiclon.So last line of a statment do not require a statement.

<?php echo "first statement";
echo
"second statement"
?>

Closing tag is optional and some cases omitting the closing tag is very helpful.
1.using include or require function
2.unwanted whitespace will not occur at the end of file
3.add header any time

<?php echo "omitting closing tag";
To Top