downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | conferences | my php.net

search for in the

HTML からの脱出> <基本的な構文
[edit] Last updated: Fri, 24 May 2013

view this page in

PHP タグ

PHP はファイルを解析して開始タグと終了タグ (<?php?>) を探します。 タグが見つかると、PHP はコードの実行を開始したり終了したりします。 このような仕組みにより、PHP を他のあらゆる形式のドキュメント中に 埋め込むことができるのです。つまり、開始タグと終了タグで囲まれている 箇所以外のすべての部分は、PHP パーサに無視されます。

PHP では、短い形式のタグ <? および ?> も使えます (しかしこれはおすすめしません。というのも、この形式のタグは php.inishort_open_tag を有効にするか --enable-short-tags オプションつきで PHP を configure した場合でないと使えないからです)。

ファイル全体が純粋な PHP コードである場合は、ファイルの最後の終了タグは省略するのがおすすめです。 終了タグの後に余分な空白や改行があると、予期せぬ挙動を引き起こす場合があるからです。 余分な空白や改行のせいで PHP が出力バッファリングを開始し、その時点の内容を意図せず出力してしまうことになります。

<?php
echo "みなさん、こんにちは";

// ... いろんなコードたち

echo "最後のごあいさつ";

// PHP 終了タグを書かずに、ここでスクリプトを終わります。



add a note add a note User Contributed Notes PHP タグ - [2 notes]
up
-21
akshay dot leadindia at gmail dot com
1 year ago
If you do (accidentally) leave out a newline character after the closing php tag ( '?>' ) then you may see 'Headers Already Sent' errors. So if you are seeing this error in your output, double check the php file for newline characters after the closing tag.
up
-6
preda dot vlad at yahoo dot com
2 months 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.

 
show source | credits | stats | sitemap | contact | advertising | mirror sites