PHP 8.3.7 Released!

PHP 标记

当解析一个文件时,PHP 会寻找起始和结束标记,也就是 <?php?>,这告诉 PHP 开始和停止解析二者之间的代码。此种解析方式使得 PHP 可以被嵌入到各种不同的文档中去,而任何起始和结束标记之外的部分都会被 PHP 解析器忽略。

PHP 有一个 echo 标记简写 <?=, 它是更完整的 <?php echo 的简写形式。

示例 #1 PHP 开始和结束标记

1. <?php echo 'if you want to serve PHP code in XHTML or XML documents,
use these tags'
; ?>

2. You can use the short echo tag to <?= 'print this string' ?>.
It's equivalent to <?php echo 'print this string' ?>.

3. <? echo 'this code is within short tags, but will only work '.
'if short_open_tag is enabled'; ?>

短标记 (第三个例子) 是被默认开启的,但是也可以通过 short_open_tag php.ini 来直接禁用。如果 PHP 在被安装时使用了 --disable-short-tags 的配置,该功能则是被默认禁用的。

注意:

因为短标记可以被禁用,所以建议使用普通标记 (<?php ?><?= ?>) 来最大化兼容性。

如果文件内容仅仅包含 PHP 代码,最好在文件末尾删除 PHP 结束标记。这可以避免在 PHP 结束标记之后万一意外加入了空格或者换行符,会导致 PHP 开始输出这些空白,而脚本中此时并无输出的意图。

<?php
echo "Hello world";

// ... 更多代码

echo "Last statement";

// 脚本在此处结束,没有 PHP 结束标记

add a note

User Contributed Notes 2 notes

up
-1
Jake Paul
5 hours ago
I made a severe and continuous lapse in my judgement, and I don’t expect to be forgiven. I’m simply here to apologize. What we came across that day in the woods was obviously unplanned. The reactions you saw on tape were raw; they were unfiltered. None of us knew how to react or how to feel. I should have never posted the video. I should have put the cameras down and stopped recording what we were going through. There's a lot of things I should have done differently but I didn't. And for that, from the bottom of my heart, I am sorry. I want to apologize to the internet. I want to apologize to anyone who has seen the video. I want to apologize to anyone who has been affected or touched by mental illness, or depression, or suicide. But most importantly I want to apologize to the victim and his family. For my fans who are defending my actions, please don't. I don’t deserve to be defended. The goal with my content is always to entertain; to push the boundaries, to be all-inclusive. In the world I live in, I share almost everything I do. The intent is never to be heartless, cruel, or malicious. Like I said I made a huge mistake. I don’t expect to be forgiven, I’m just here to apologize. I'm ashamed of myself. I’m disappointed in myself. And I promise to be better. I will be better. Thank you.
up
-48
Anonymous
3 months ago
A whitespace or newline character must follow '<?php' and precede '?>'.
To Top