当解析一个文件时,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 tags (example three) are available by default but can be disabled either via the short_open_tag php.ini configuration file directive, or are disabled by default if PHP is built with the --disable-short-tags configuration.
注意:
As short tags can be disabled it is recommened to only use the normal tags (
<?php ?>and<?= ?>) to maximise compatibility.
如果文件内容仅仅包含 PHP 代码,最好在文件末尾删除 PHP 结束标记。这可以避免在 PHP 结束标记之后万一意外加入了空格或者换行符,会导致 PHP 开始输出这些空白,而脚本中此时并无输出的意图。
<?php
echo "Hello world";
// ... more code
echo "Last statement";
// 脚本至此结束,并无 PHP 结束标记