PHP タグ

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

PHP では、短い形式のechoタグ <?= も使えます。 これは、より冗長な <?php echo を短くしたものです。

例1 PHP の開始タグと終了タグ

1. <?php echo 'XHTMLまたはXMLドキュメントの中でPHPコードを扱いたい場合は、このタグを使いましょう'; ?>

2. 短い形式の echo タグを使って <?= 'この文字列を表示' ?> とすることもできます。
これは <?php echo 'この文字列を表示' ?> と同じ意味になります。

3. <? echo 'このコードは短縮型のタグに囲まれていますが、'.
'short_open_tag が有効な場合にしか動作しません'; ?>

短縮型のタグ(例 3.)はデフォルトで有効ですが、 php.ini 設定ファイルのディレクティブ short_open_tag で無効にすることもできますし、 --disable-short-tags オプション付きで configure した場合は、 デフォルトで無効にすることも出来ます。

注意:

短縮形のタグは無効にすることができるので、 互換性を最大限保つために、通常のタグ (<?php ?> and <?= ?>) を使うことを推奨します。

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

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

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

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

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

add a note

User Contributed Notes 2 notes

up
-3
irfan dot swen at gmail dot com
1 month ago
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h1>PHP Language</h1>
    <?php echo "1) PHP Special Syntax Code"; ?><br>

    <?= "2) PHP Special Syntax Code"; ?><br>

    <!-- Not Working-->  <? "3) PHP Special Syntax Code" ?> 
</body>
</html>
up
-59
anisgazig at gmail dot com
1 year 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;
?>
To Top