PHP 标记

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

PHP 也允许使用短标记 <??>,但不鼓励使用。只有通过激活 php.ini 中的 short_open_tag 配置指令或者在编译 PHP 时使用了配置选项 --enable-short-tags 时才能使用短标记。

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

<?php
echo "Hello world";

// ... more code

echo "Last statement";

// 脚本至此结束,并无 PHP 结束标记

add a note

User Contributed Notes 6 notes

up
4
crazytonyi at gmail dot com
2 months ago
Regarding earlier note by @purkrt :

> I would like to stress out that the opening tag is "<?php[whitespace]", not just "<?php"

This is absolutely correct, but the wording may confuse some developers less familiar with the extent of the term "[whitespace]".

Whitespace, in this context, would be any character that generated vertical or horizontal space, including tabs ( \t ), newlines ( \n ), and carriage returns ( \r ), as well as a space character ( \s ). So reusing purkrt's example:

<?php/*blah*/ echo "a"?>

would not work, as mentioned, but :

<?php /*php followed by space*/ echo "a"?>

will work, as well as :

<?php
/*php followed by end-of-line*/ echo "a"?>

and :

<?php    /*php followed by tab*/ echo "a"?>

I just wanted to clarify this to prevent anyone from misreading purkrt's note to mean that a the opening tag --even when being on its own line--required a space ( \s ) character. The following would work but is not at all necessary or how the earlier comment should be interpreted :

<?php
/*php followed by a space and end-of-line*/ echo "a"?>

The end-of-line character is whitespace, so it is all that you would need.
up
19
purkrt at gmail dot com
1 year ago
I would like to stress out that the opening tag is "<?php[whitespace]", not just "<?php". While this might seem blatantly obvious, I thought for some time that

<?php/*blah*/ echo "a"?>

would work, and it does not; the comment does not work as whitespace. I've run into this while converting some older code with short open tag.
up
-1
jcastromail at yahoo dot es
4 months ago
Its important:

this code (for web)
    <pre>
    I
    <? echo "am;" ?>
    Groot
    </pre>

Generates the next result:
    I
    amGroot
instead of
    I
    am
    Groot
The last tag "?>" deletes the end of the line.

However, if we changed the line
    <? echo "am"; ?>
by
    <? echo "am"; ?>.  (or  a space)
then the result is :
    I
    am.
    Groot
up
-78
mario
1 year ago
A few related notes, partly covered elsewhere in the manual:

  → Since PHP 5.4 the inline echo <?= ?> short tags are always
    enabled regardless of the short_open_tag (php.ini) setting.

  → PHP tags are infrequently also referred to as open/close "tokens"
    (as per the tokenizers T_OPEN_TAG / _ECHO, and T_CLOSE_TAG naming).

  → The historic ASP-style <% %> and even more rarely used
    <script language=PHP></script> tags are to be repealed in PHP7.
    http://wiki.php.net/rfc/remove_alternative_php_tags

There also exists a small tool called "phptags tidier" for consistently rewriting PHP short/long tags.  It's suitable to normalize include and template scripts, e.g. employ the always-enabled long <?php ?> tags, and/or relieve whitespace padding before/after PHP tags:

    phptags --long --whitespace --warn  *.php

Instead of reliably fixing the common whitespace/BOM issues around tags, it can also just remove all ?> close tags with `--unclose --tokenizer` as advised afore.
up
-86
alexander dot podgorny at somewhere dot com
1 year ago
One reason to use long tags over short is to avoid confusion with <?xml ?> notation.
up
-161
preda dot vlad at yahoo dot com
3 years ago
So here are the valid ways to open PHP tags:

<?php ?> // standard tags
<? ?> // short tags, need short_open_tag enabled in php.ini
<% %> // asp tags, need asp_tags enabled in php.ini
<script language="php"> </script> // case insensitive

PSR-1 coding standards suggest to only use <?php ?> or <?= ?> (echo short tags) - and no other variations, and PSR-2 suggests to not close the tags in PHP only files.