©
Dieses Dokument verwendet PHP-Handbuch für chinesische Websites Freigeben
凡是在一对开始和结束标记之外的内容都会被 PHP 解析器忽略,这使得 PHP 文件可以具备混合内容。 可以使 PHP 嵌入到 HTML 文档中去,如下例所示。
<p>This is going to be ignored by PHP and displayed by the browser.</p>
<?php echo 'While this is going to be parsed.' ; ?>
<p>This will also be ignored by PHP and displayed by the browser.</p>
使用条件结构:
Example #1 使用条件的高级分离术
<?php if ( $expression == true ): ?>
This will show if the expression is true.
<?php else: ?>
Otherwise this will show.
<?php endif; ?>
要输出大段文本时,跳出 PHP 解析模式通常比将文本通过 echo 或 print 输出更有效率。
可以在 PHP 中使用四对不同的开始和结束标记。其中两种,<?php ?> 和 <script language="php"> </script> 总是可用的。另两种是短标记和 ASP 风格标记,可以在 php.ini 配置文件中打开或关闭。尽管有些人觉得短标记和 ASP 风格标记很方便,但移植性较差,通常不推荐使用。
Note:
此外注意如果将 PHP 嵌入到 XML 或 XHTML 中则需要使用 <?php ?> 标记以保持符合标准。
Example #2 PHP 开始和结束标记
1. <?php echo 'if you want to serve XHTML or XML documents, do it like this' ; ?>
2. <script language="php">
echo 'some editors (like FrontPage) don\'t
like processing instructions' ;
</script>
3. <? echo 'this is the simplest, an SGML processing instruction' ; ?>
<?= expression ?> This is a shortcut for "<? echo expression ?>"
4. <% echo 'You may optionally use ASP-style tags'; %>
<%= $variable; # This is a shortcut for "<% echo . . ." %>
上例中的 1 和 2 中使用的标记总是可用的,其中示例 1 中是最常用,并建议使用的。
短标记(上例 3)仅在通过 php.ini 配置文件中的指令 short_open_tag 打开后才可用,或者在 PHP 编译时加入了 --enable-short-tags 选项。
ASP 风格标记(上例 4)仅在通过 php.ini 配置文件中的指令 asp_tags 打开后才可用。
Note:
在以下情况应避免使用短标记:开发需要再次发布的程序或者库,或者在用户不能控制的服务器上开发。因为目标服务器可能不支持短标记。为了代码的移植及发行,确保不要使用短标记。
Note:
在 PHP 5.2 和之前的版本中,解释器不允许一个文件的全部内容就是一个开始标记 <?php。自 PHP 5.3 起则允许此种文件,但要开始标记后有一个或更多白空格符。
Note:
自 PHP 5.4 起,短格式的 echo 标记 <?= 总会被识别并且合法,而不管 short_open_tag 的设置是什么。
[#1] nath dot realmadrid at gmail dot com [2015-06-30 07:21:38]
" There are four different pairs of opening and closing tags which can be used in PHP. Two of those,
<?php ?>
and <script language="php"> </script>, are always available " - It is not true with PHP7. script and ASP style tags are removed from PHP7.
[#2] forums at tamp dot it [2015-01-12 18:37:42]
The German version of this documentation page is incorrect. I didn't understand what it wanted to tell me, so I went to the English version, which is clear.
The Problem is that the section "dies arbeitet wie erwartet, da PHP, wenn..." follows the advanced escaping example in the German version, whereas it precedes the example in the English version. The English version then has its own (proper) explanation of the example, whereas the German version has none.
[#3] Kalimuthu [2014-09-06 09:23:28]
One more disadvantage of using PHP short tags is that, it clashes with XML. Because XML also uses
<?php to open code blocks. See below sample XML code
<?phpxml version="1.0" encoding="UTF-8"?>
<note>
<to> Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
[#4] sgurukrupa at gmail dot com [2014-03-10 07:01:32]
Although not specifically pointed out in the main text, escaping from HTML also applies to other control statements:
<?php for ($i = 0; $i < 5; ++$i): ?>
Hello, there!
<?php endfor; ?>
When the above code snippet is executed we get the following output:
Hello, there!
Hello, there!
Hello, there!
Hello, there!
[#5] mike at clove dot com [2010-11-30 10:30:58]
It's possible to write code to create php escapes which can be processed later by substituting \x3f for '?' - as in echo "<\x3fphp echo 'foo'; \x3f>";
This is useful for creating a template parser which later is rendered by PHP.
[#6] quickfur at quickfur dot ath dot cx [2010-07-26 14:40:16]
When the documentation says that the PHP parser ignores everything outside the
<?php ... ?>
tags, it means literally EVERYTHING. Including things you normally wouldn't consider "valid", such as the following:
<html><body>
<p
<?php if ($highlight): ?>
class="highlight"
<?php endif;?>
>This is a paragraph.</p>
</body></html>
Notice how the PHP code is embedded in the middle of an HTML opening tag. The PHP parser doesn't care that it's in the middle of an opening tag, and doesn't require that it be closed. It also doesn't care that after the closing ?> tag is the end of the HTML opening tag. So, if $highlight is true, then the output will be:
<html><body>
<p class="highlight">This is a paragraph.</p>
</body></html>
Otherwise, it will be:
<html><body>
<p>This is a paragraph.</p>
</body></html>
Using this method, you can have HTML tags with optional attributes, depending on some PHP condition. Extremely flexible and useful!
[#7] snor_007 at hotmail dot com [2010-04-01 16:28:43]
Playing around with different open and close tags I discovered you can actually mix different style open/close tags
some examples
<%
//your php code here
?>
or
<script language="php">
//php code here
%>
[#8] ravenswd at gmail dot com [2009-08-01 17:08:12]
One aspect of PHP that you need to be careful of, is that ?> will drop you out of PHP code and into HTML even if it appears inside a // comment. (This does not apply to comments.) This can lead to unexpected results. For example, take this line:
<?php
' . "\n";
$file_contents = '<?php die(); ?>
?>
If you try to remove it by turning it into a comment, you get this:
<?php
// $file_contents = '<?php die(); ?>' . "\n";
?>
Which results in ' . "\n"; (and whatever is in the lines following it) to be output to your HTML page.
The cure is to either comment it out using tags, or re-write the line as:
<?php
$file_contents = '<' . '?php die(); ?' . '>' . "\n";
?>
[#9] david dot jarry at gmail dot com [2009-03-26 03:40:54]
Shorts tags and ASP tags are unportables and should be avoided.
<script /> tags are a waste of time and simply inefficient in some simple cases :
<body>
<p style="color: <script language="php"> echo $text_color </script>;">
(...) VERY long text (...)
</p>
</body>
To render this example in a basic XHTML editor, you need to "echo()" all the content or break the XML rules.
The solution seems obvious to me : Why not add the shortcut "
<?php= ?>
" to be used within XML and XHTML documents ?
<?php='example1'?>
<?php=$example2?>
[#10] admin at furutsuzeru dot net [2009-01-02 08:50:10]
These methods are just messy. Short-opening tags and ASP-styled tags are not always enabled on servers. The <script language="php"></script> alternative is just out there. You should just use the traditional tag opening:
<?php?>
Coding islands, for example:
<?php
$me = 'Pyornide';
?>
<?php=$me;?>
is happy.
<?php
$me = strtoupper($me);
?>
<?php=$me;?>
is happier.
Lead to something along the lines of messy code. Writing your application like this can just prove to be more of an
inconvenience when it comes to maintenance.
If you have to deal chunks of HTML, then consider having a templating system do the job for you. It is a poor idea to rely on the coding islands method as a template system in any way, and for reasons listed above.