1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
|
<?xml version="1.0" encoding="utf-8"?>
<chapter id="language.basic-syntax">
<title>基本的な構文</title>
<!--
NOTE: Last modified: 2001-05-16 13:00 GMT
the language part is currently under heavy revision. Please do not
not make any heavy (i.e. structural) modifications to this part
for a moment.
You'd also better not start any translation yet.
Comments are always welcome at phpdoc@lists.php.net
Progress:
intro : DOESN'T EXIST - yet?
new chapter, with some introductionary remarks?
Will be discussed on the ML soon.
basic-syntax:
FINISHED
except maybe moving the 'advanced escaping'
to a better place?
TODO:
- nada
types : Being revised. Added all new types
Boolean and Integer are more or less finished.
The rest isn't.
TODO:
- why is $foo[bar] bad syntax?
- what's the difference between unset($bla) and
$bla = NULL; (it is different!)
- $obj->{expr} syntax
- (unset) cast?????
- $bla = unset <== should've been nuked, don't mention it
- $str{offset} syntax, rather than $str[offset]
- read notes and apply when any of them are useful
- remove notes which have been included here.
- ...
the rest: Not yet started with.
TODO:
- ?
oop : has been revised by Kristian, DONE.
-->
<sect1 id="language.basic-syntax.phpmode">
<title>HTML からの脱出</title>
<para>
PHPはファイルからPHPコードの開始を示す特別なタグを見つけるまでは
単純にその内容を出力します。PHPコードの開始を示すタグが見つかると
PHPはコードの終了を示すタグとの間にあるコードを実行します。この
メカニズムによりPHPコードをHTMLの中に埋め込むことが出来るのです。
つまり、PHPタグの中にある文字列はPHPコードとして解釈されますが、
外にある文字列(つまり普通のHTML)はあくまで単純に出力されるだけ
ということです。
</para>
<para>
PHPコードのブロックであることを宣言するタグは4種類あります。
これらのうちの2つ((<?php. . .?>と<script language="php">
. . .</script>)は常に使用することが出来ます。その他の
タグは<filename>php.ini</filename>設定ファイルでonまたはoffに
するとが出来ます。短縮型のタグ(<?. . .?>)とASP形式の
タグは便利なのですが、長いタグに比べると移植性に欠けます。
また、PHPコードをXMLやXHTMLに埋め込もうとする場合にはXMLの
規格に従うために<?php. . .?>形式を使用する必要があるでしょう。
</para>
<para>
PHPでサポートされるタグは以下のとおりです:
</para>
<para>
<example>
<title>HTMLからの脱出法</title>
<programlisting role="php">
<![CDATA[
1. <? echo ("この方法は最も簡単で、SGMLの処理命令です\n"); ?>
2. <?php echo("XHTMLまたはXMLドキュメントを処理したい場合は、この方法が良いでしょう\n"); ?>
3. <script language="php">
echo ("(FrontPageのような) いくつかのエディタ は処理命令を好み
ません");
</script>
4. <% echo ("オプションでASP形式のタグを使用可能です"); %>
<%= $variable; # これは、"<%echo .." のショートカットです。%>
]]>
</programlisting>
</example>
</para>
<para>
最初の方法は、短縮型のタグが有効な場合にのみ利用可能です。
短縮型のタグは、<function>short_tags</function> 関数を使用するか
(PHP 3のみ)、 PHPの設定ファイルに
<link linkend="ini.short-open-tag">short_open_tag</link>を設定する
か--enable-short-tags オプションを <command>configure</command> に
指定して PHP をコンパイルすることにより、有効にできます。
</para>
<para>
2番目の方法は、次世代のXHTMLをPHPで容易に実装することが可能とする
ので、好ましい方法です。
</para>
<para>
4番目の方法は、ASP型式のタグが
<link linkend="ini.asp-tags">asp_tags</link> の設定により有効となっ
ている場合にのみ使用可能です。
<note>
<para>
ASP 型のタグのサポートは、3.0.4で追加されました。
</para>
</note>
</para>
<para>
ブロックの終了タグは、直後に改行がある場合、それを含ん
だものになります。また、終了タグには自動的にセミコロン
が含まれていると認識されます。従ってPHPコードの最終行
にはセミコロンを記述する必要はありません。
</para>
<para>
PHP は、次のような構造を使用可能です。
<example><title>高度なエスケープ処理</title>
<programlisting role="php">
<![CDATA[
<?php
if ( boolean-expression )
{
?>
<strong>This is true.</strong>
<?php
}
else
{
?>
<strong>This is false.</strong>
<?php
}
?>
]]>
</programlisting>
</example>
このコードは期待通りに動作します。これは、PHPが?> および
<?phpの中のテキストを<function>echo</function>命令として処理す
るためです。この例はもちろん不自然なものですがPHPモードを抜ける
ことでテキストを出力させる方法は、それらを<function>echo</function>
や<function>print</function>等を使って出力させるより便利です。
</para>
</sect1>
<sect1 id="language.basic-syntax.instruction-separation">
<title>命令の分離</title>
<simpara>
命令は、C や Perl と同様に分離されます。つまり、各文はセミコロンで
終了します。
</simpara>
<para>
閉じるタグ (?>) も文の終わりの意味を有します。このため、以下の
文は等価です。
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
echo "This is a test";
?>
<?php echo "This is a test" ?>
]]>
</programlisting>
</informalexample>
</para>
</sect1>
<sect1 id="language.basic-syntax.comments">
<title>コメント</title>
<para>
PHP は、'C', 'C++' および Unix シェル型のコメントをサポートします。
例えば、
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
echo "This is a test"; // C++型の単一行用のコメント
/* 複数行用のコメント
もう一行分のコメント */
echo "This is yet another test";
echo "One Final Test"; # シェル型のコメント
?>
]]>
</programlisting>
</informalexample>
</para>
<simpara>
"一行"コメントは、改行またはPHPコードのブロックの終わり
のうちどちらか最初にくる方までです。
</simpara>
<informalexample>
<programlisting role="php">
<![CDATA[
<h1>This is an <?php # echo "simple";?> example.</h1>
<p>The header above will say 'This is an example'.
]]>
</programlisting>
</informalexample>
<simpara>
'C'型のコメントがネストしないように注意する必要があります。
ネストは、大きなブロックをコメントアウトする際に生じます。
</simpara>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
/*
echo "This is a test"; /* このコメントが問題を生じます */
*/
?>
]]>
</programlisting>
</informalexample>
</sect1>
</chapter>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"../../manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
-->
|