mirror of
https://github.com/php/doc-ja.git
synced 2026-03-23 22:52:11 +01:00
122 lines
3.7 KiB
XML
122 lines
3.7 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
|
<!-- $Revision$ -->
|
|
<!-- EN-Revision: 22583751fbfdaa3eaa41aeb6470d1343f5cb2c78 Maintainer: takagi Status: ready -->
|
|
|
|
<sect1 xml:id="control-structures.alternative-syntax" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
|
|
<title>制御構造に関する別の構文</title>
|
|
<?phpdoc print-version-for="control-structures.alternative-syntax"?>
|
|
<para>
|
|
PHPは、いくつかの制御構造、つまり、<literal>if</literal>、
|
|
<literal>while</literal>、<literal>for</literal>、
|
|
<literal>foreach</literal>、<literal>switch</literal>
|
|
に関する別の構文を提供します。
|
|
各構造において開き波括弧をコロン(:)、閉じ波括弧をそれぞれ
|
|
<literal>endif;</literal>,<literal>endwhile;</literal>,
|
|
<literal>endfor;</literal>,<literal>endforeach;</literal>,
|
|
<literal>endswitch;</literal>に変更するのが
|
|
別の構文の基本的な形式となります。
|
|
<informalexample>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php if ($a == 5): ?>
|
|
Aは5に等しい
|
|
<?php endif; ?>
|
|
]]>
|
|
</programlisting>
|
|
</informalexample>
|
|
</para>
|
|
<simpara>
|
|
上の例では、HTML ブロック "Aは5に等しい" はこの構文で
|
|
書かれた <literal>if</literal> 文の内部で入れ子になっています。
|
|
この HTML ブロックは、<varname>$a</varname> が 5 の場合にのみ表示されます。
|
|
</simpara>
|
|
<para>
|
|
この方法は、<literal>else</literal> や <literal>elseif</literal>
|
|
にも同様に適用することができます。
|
|
次の例は、この形式で <literal>if</literal> 文を <literal>elseif</literal>
|
|
および <literal>else</literal> とともに使用しています。
|
|
<informalexample>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
if ($a == 5):
|
|
echo "aは5に等しい";
|
|
echo "...";
|
|
elseif ($a == 6):
|
|
echo "aは6に等しい";
|
|
echo "!!!";
|
|
else:
|
|
echo "aは5でも6でもない";
|
|
endif;
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
</informalexample>
|
|
</para>
|
|
<note>
|
|
<para>
|
|
同じブロック内で別の構文を混ぜて使うことはできません。
|
|
</para>
|
|
</note>
|
|
<warning>
|
|
<para>
|
|
<literal>switch</literal> 文と最初の
|
|
<literal>case</literal> の間で何か (空白文字も含む) を出力すると、構文エラーになります。
|
|
たとえば、以下のコードは無効です。
|
|
</para>
|
|
<informalexample>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php switch ($foo): ?>
|
|
<?php case 1: ?>
|
|
// ...
|
|
<?php endswitch; ?>
|
|
]]>
|
|
</programlisting>
|
|
</informalexample>
|
|
<para>
|
|
一方、以下のコードは有効です。というのも、
|
|
<literal>switch</literal> 文の行末の改行文字は終了タグ
|
|
<literal>?></literal> の一部と見なされるため、
|
|
<literal>switch</literal> と <literal>case</literal>
|
|
の間では何も出力されないからです。
|
|
</para>
|
|
<informalexample>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php switch ($foo): ?>
|
|
<?php case 1: ?>
|
|
...
|
|
<?php endswitch; ?>
|
|
]]>
|
|
</programlisting>
|
|
</informalexample>
|
|
</warning>
|
|
<para>
|
|
<link linkend="control-structures.while">while</link>、
|
|
<link linkend="control-structures.for">for</link>、および <link
|
|
linkend="control-structures.if">if</link> に、より多くの例があります。
|
|
</para>
|
|
</sect1>
|
|
|
|
<!-- 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:"~/.phpdoc/manual.ced"
|
|
sgml-exposed-tags:nil
|
|
sgml-local-catalogs:nil
|
|
sgml-local-ecat-files:nil
|
|
End:
|
|
vim600: syn=xml fen fdm=syntax fdl=2 si
|
|
vim: et tw=78 syn=sgml
|
|
vi: ts=1 sw=1
|
|
-->
|