指令分隔

與 C 或 Perl 一樣,PHP 要求指令在每個語句的末尾用分號終止。PHP 程式碼塊的結束標記會自動隱含分號;您不需要在 PHP 程式碼塊的最後一行使用分號來終止。程式碼塊的結束標記將包含緊隨其後的換行符(如果有)。

示例 #1 顯示結束標記包含尾隨換行符的示例

<?php echo "Some text"; ?>
無換行符
<?= "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 程式碼塊的結束標記是可選的,在某些情況下省略它在使用 includerequire 時很有用,這樣就不會在檔案末尾出現不需要的空白,並且您仍然可以稍後將標頭新增到響應中。如果您使用輸出緩衝,並且不想在包含檔案生成的部件末尾看到新增的不必要的空白,這也很方便。

新增說明

使用者貢獻說明 3 條說明

57
Krishna Srikanth
17 年前
不要誤解

<?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.
1
M1001
2 年前
您也可以在一行中編寫多個語句,只需用分號隔開,例如

<?php
echo "a"; echo "b"; echo "c";
#The output will be "abc" with no errors
?>
0
moonlander12341234 at gmail dot com
4 個月前
一位來自 stack overflow 的使用者對尾隨換行符有一個很好的解釋,簡單來說,

<?= "Hello" ?>
Jello

將輸出:

HelloJello

這意味著來自 ?> 標記的隱式換行符不存在,但是您可以簡單地將其新增到程式碼中,例如:

<?= "Hello" ?>

Jello

結束標記後的空格充當換行符
To Top