PHP 8.3.4 Released!

HTML からの脱出

PHP のパーサは、開始タグと終了タグに囲まれていない部分をすべて無視します。 そのおかげで、PHP のファイルにそれ以外のコンテンツを混在させることができるのです。 たとえば PHP を HTML ドキュメントに組み込んで、テンプレートを作ったりすることもできます。

<p>この部分は PHP から無視され、そのままブラウザには表示されます。</p>
<?php echo '一方、この部分はパースされます。'; ?>
<p>この部分も PHP から無視され、そのままブラウザには表示されます。</p>
これは期待通りに動作します。なぜなら、PHP インタプリタは ?> 終了タグを見つけると それ以降新たに開始タグを見つけるまでの内容を何でも出力するからです (終了タグの直後の改行は別です。 命令の分離 を参照ください)。 しかし、PHP が条件文の中にいる場合は話が別です。 その場合は、まず条件式の結果を判定してから何をスキップするかを判断します。 次の例を参照ください。

条件文を使った例です。

例1 条件文を使った高度な脱出

<?php if ($expression == true): ?>
条件式が真の場合にこれが表示されます。
<?php else: ?>
それ以外の場合にこちらが表示されます。
<?php endif; ?>
この例では、PHP は条件を満たさないブロックを処理しません。たとえそれが PHP の開始タグと終了タグに囲まれた部分でなくても、条件文にしたがってそこを読み飛ばします。 というのも、PHP のインタプリタは条件を満たさない箇所をブロックごと読み飛ばすからです。

大量のテキストを出力する際に echoprint を用いることを考えると、このように一度 PHP のパースモードを抜けるほうが効率的です。

PHP コードを XML や XHTML に 埋め込む場合には、標準規格に従うために <?php ?> タグを使用する 必要があるでしょう。

add a note

User Contributed Notes 4 notes

up
383
quickfur at quickfur dot ath dot cx
13 years ago
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!
up
80
ravenswd at gmail dot com
14 years ago
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
$file_contents
= '<?php die(); ?>' . "\n";
?>

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";
?>
up
14
sgurukrupa at gmail dot com
10 years ago
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!
up
-1
gazianis005 at gmail dot com
11 months ago
There are two types of escaping from HTML.
1.Normal escaping of using outside of a pair of opening and closing tag
2.Advance escaping of using conditions.

Example of normal escaping

<p>This is going to be ignored by the php parser and displayed by the browser</p>
<?php echo 'This is going to be parsed';?>
<>This will also ignored by the php parser and displayed by the browser</>

Example of advanced escaping

<?php if($expression == true): ?>
This will show if expression is true
<?php else : ?>
Otherwise this will show
<?php endif ;?>
To Top