指令分隔符

同 C 或 Perl 一樣,PHP 需要在每個語句後用分號結束指令。一段 PHP 代碼中的結束標籤隱含表示了一個分號;在一個 PHP 代碼段中的最後一行可以不用分號結束。如果後面還有新行,則程式區段的結束標籤包含了行結束標籤。

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

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

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

Note:

文件末尾的 PHP 代碼段結束標籤是選擇性的,有些情況下當使用 include 或者 require 時省略掉會更好些,這樣不期望的空格就不會出現在文件末尾,之後仍然可以輸出回覆表頭。在使用輸出緩衝時也很便利,就不會看到由包含文件生成的不期望的空格。

add a note add a note

User Contributed Notes 2 notes

up
110
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
9
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