略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: PHP 标记

2024-04-30

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 noteadd a note

User Contributed Notes 3 notes

up
-2
anisgazig at gmail dot com
7 months ago
If you want your file to be interpreted as php then your file must start and end with <?php and ?> and everything outside of that is ignored by the php parser.

<?php
php code
..//parsed
php code..//parsed
?>
hellow..//normal test but ignred by php parser

Three types of tag are available in php
1.normal tag(<?php ?>)
2.short echo tag(<?= ?>)
3.short tag(<? ?>)

short tag are bydefault available but can be disabled by short_open_tag = Off and also disabled bydefault if php will  built with --disabe--short--tags()

As short tag can be disabled so only use the normal and short echo tag.

If your file only have php code then  do not use closing tag.
<?php
//php code;
//php code;
//php code;

but if you are embedding php with html then enclose php code with opening and closing tag.
<
html>
<
head>
</
head>
<
body>
<?
php
//php code;
//php code;
//php code;

?>
</body>
</html>

If you want to just print single text or something ,you should use shorthand version .<?= $var ?>

But if you want to process something, you should use normal tag.
<?php
       
//$var = 3;
        //$var2 = 2;
        //$var3 = $var+$var2;
        //if($var3){//result}

?>

If you embedded php with html and single line, do not need to use semicolon
<html>
<head>
<body>
<?= $var ?>
</body>
</head>
</html>
but if you have multiple line, then use semicolon.
<?php
//line 1;
//line 2;
//line 3;
?>
up
-24
anisgazig at gmail dot com
7 months ago
There is no defference between normal(<?php ?>) and short echo tag() but without uses of comments.
example:

<h1>Normal tag with c++ style oneline comment:  <?php //"Normal tag"; ?> breaks php mode and return html mode  </h1>

<h1>html code after (normal tag)<?php // and commnet then (closing tag) ?> breaks php mode and return html mode</h1>

but in short echo tag
<h1>html code after (short echo tag)<?php // and commnet then (closing tag) ?> breaks php mode and does not return html mode</h1>

Same rules are applied within # and /**/ style comments.
up
-57
admin at bharatt dot com dot np
1 year ago
You may want to know that removing semicolon is optional sometime but need to know the condition when it can be removed and when it can't be.
-------------------------------------------------------------------------
// Example 1: PHP script with closing tag at the end.
<?php

// php code

// you can remove semicolon
mysqli_close( $db )
?>

// Example 2: PHP script without closing tag at the end.
<?php

// php code

// you can't remove semicolon
mysqli_close( $db )

-----------------------------------------------------------------------

官方地址:https://www.php.net/manual/en/language.basic-syntax.phptags.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3