「条件分岐」の編集履歴(バックアップ)一覧はこちら

条件分岐」(2008/10/27 (月) 10:34:59) の最新版変更点

追加された行は緑色になります。

削除された行は赤色になります。

<h3>条件分岐</h3> <p> <font color="#000080" size="4" style="background-color:rgb(255,204,153);">算術演算子</font></p> <h4 style="background-color:rgb(255,153,204);">if文</h4> <table cellspacing="1" cellpadding="1" border="0" width="586"><tbody><tr><td style="background-color:rgb(192,192,192);">&lt;?php</td> <td style="background-color:rgb(204,255,255);">▼▼▼説明▼▼▼</td> </tr><tr><td style="background-color:rgb(192,192,192);">$a = 200;</td> <td style="background-color:rgb(204,255,255);"> 変数$aに200を代入</td> </tr><tr><td style="background-color:rgb(192,192,192);">if ($a &gt; 200) {</td> <td style="background-color:rgb(204,255,255);"> $aは200より大きいの?</td> </tr><tr><td style="background-color:rgb(192,192,192);">    print &quot;値は200より大きいです。&quot;;</td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">} elseif ($a &gt; 100) {</td> <td style="background-color:rgb(204,255,255);"> $aは100より大きいの?</td> </tr><tr><td style="background-color:rgb(192,192,192);">    print &quot;値は200以下で100より大きいです。&quot;;</td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">} else {</td> <td style="background-color:rgb(204,255,255);"> $aは上記以外なの?</td> </tr><tr><td style="background-color:rgb(192,192,192);">    print &quot;値は100以下です。&quot;;</td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">}</td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">?&gt;</td> <td style="background-color:rgb(204,255,255);"> </td> </tr></tbody></table><p> ◆三項演算子<br /><code><span style="color:rgb(0,0,0);"><span style="color:rgb(0,0,187);">$bool =</span><span style="color:rgb(0,119,0);">($a == 200</span><span style="color:rgb(0,119,0);">) ?</span><span style="color:rgb(221,0,0);">'200です'</span><span style="color:rgb(0,119,0);">:</span></span></code><code><span style="color:rgb(0,0,0);"><span style="color:rgb(221,0,0);">'200じゃないです'</span></span></code><code><span style="color:rgb(0,0,0);"><span style="color:rgb(0,119,0);">;</span></span></code><br /> これは、if文の簡単な書き方で、もし$aが200なら変数$boolに「200です」を代入し、それ以外は「200じゃないです」を代入する書き方です.</p> <h4 style="background-color:rgb(255,153,204);">比較演算子</h4> <table cellspacing="1" cellpadding="1" border="0" width="586"><tbody><tr><td style="background-color:rgb(192,192,192);">比較条件</td> <td style="background-color:rgb(192,192,192);">比較演算子</td> <td style="background-color:rgb(192,192,192);">用例</td> </tr><tr><td>等しい</td> <td>  ==</td> <td>  $a == $b(aとbの値が等しいならtrue)</td> </tr><tr><td>~以上</td> <td>  &gt;=</td> <td>  $a &gt;= $b(aの値がb以上ならtrue)</td> </tr><tr><td>~より大きい</td> <td>  &gt;</td> <td>  $a &gt; $b(aの値がbより大きいならtrue)</td> </tr><tr><td> ~以下</td> <td>  &lt;=</td> <td>  $a &lt;= $b(aの値がb以下ならtrue)</td> </tr><tr><td> より小さい</td> <td>  &lt;</td> <td>  $a &lt; $b(aの値がbより小さいならtrue)</td> </tr><tr><td> 等しくない</td> <td>  != または &lt;&gt;</td> <td>  $a != $b(aの値が異なればtrue)</td> </tr></tbody></table><p> </p> <h4 style="background-color:rgb(255,153,204);">論理演算子</h4> <p> </p> <table cellspacing="1" cellpadding="1" border="0" width="599"><tbody><tr><td style="background-color:rgb(192,192,192);">比較条件</td> <td style="background-color:rgb(192,192,192);">論理演算子</td> <td style="background-color:rgb(192,192,192);">比較の仕方</td> </tr><tr><td>論理積</td> <td> and または &amp;&amp;</td> <td>条件式がすべてtrueなら、全体としてtrue</td> </tr><tr><td>論理和</td> <td> or または ||</td> <td>条件式の内1つがtrueなら、全体としてtrue</td> </tr><tr><td>否定</td> <td> !</td> <td>【 ! 】の次の条件式がfalseなら、全体としてtrue</td> </tr><tr><td>排他的論理和</td> <td> xor</td> <td>前後条件式の一方がtrueでもう一方がfalseなら、全体としてtrue</td> </tr></tbody></table><p> <font color="#FF0000">サンプル</font></p> <table cellspacing="1" cellpadding="1" border="0" width="592"><tbody><tr><td style="background-color:rgb(192,192,192);">&lt;?php</td> <td style="background-color:rgb(204,255,255);"> ▼▼▼説明▼▼▼</td> </tr><tr><td style="background-color:rgb(192,192,192);">    $a = 123;</td> <td style="background-color:rgb(204,255,255);"> 変数$aに123を代入</td> </tr><tr><td style="background-color:rgb(192,192,192);">    $b = 101;</td> <td style="background-color:rgb(204,255,255);"> 変数$bに101を代入</td> </tr><tr><td style="background-color:rgb(192,192,192);"> </td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">    if ($a &gt; 100<font color="#FF0000">and</font>$a &gt; $b) {</td> <td style="background-color:rgb(204,255,255);"> 二つの式がtrueなら、全体としてtrue</td> </tr><tr><td style="background-color:rgb(192,192,192);">        print  &quot;$aは100より大きくかつ$bより大きい&quot;;</td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">    }</td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">    if ($a &gt; 100<font color="#FF0000">or</font>$b &gt; 100) {</td> <td style="background-color:rgb(204,255,255);">  二つの式の一つがtrueなら、全体としてtrue</td> </tr><tr><td style="background-color:rgb(192,192,192);">        print  &quot;$aと$bのいずれかは100より大きい&quot;;</td> <td style="background-color:rgb(204,255,255);">  二つの式ともtrueの場合でも、全体としてtrue</td> </tr><tr><td style="background-color:rgb(192,192,192);">    }</td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">    if (<font color="#FF0000">!</font>($a == 100)) {</td> <td style="background-color:rgb(204,255,255);"> $aが100<font color="#FF0000">ではないなら</font>、全体としてtrue</td> </tr><tr><td style="background-color:rgb(192,192,192);">        print  &quot;$aは100ではない&quot;;</td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">    }</td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">?&gt;</td> <td style="background-color:rgb(204,255,255);"> </td> </tr></tbody></table><p> </p> <h4 style="background-color:rgb(255,153,204);">switch文</h4> <p> </p> <table cellspacing="1" cellpadding="1" border="0" width="605"><tbody><tr><td style="background-color:rgb(192,192,192);">&lt;?php</td> <td style="background-color:rgb(204,255,255);">▼▼▼説明▼▼▼</td> </tr><tr><td style="background-color:rgb(192,192,192);">    $a = 2;</td> <td style="background-color:rgb(204,255,255);">変数$aに2を代入</td> </tr><tr><td style="background-color:rgb(192,192,192);"> </td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">    switch ($a) {</td> <td style="background-color:rgb(204,255,255);">switch文のより$aの値を比較</td> </tr><tr><td style="background-color:rgb(192,192,192);">        case 1:</td> <td style="background-color:rgb(204,255,255);">case 文の末尾は【 : 】(コロン)</td> </tr><tr><td style="background-color:rgb(192,192,192);">            print  &quot;$aの値は1です&quot;;</td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">            break;</td> <td style="background-color:rgb(204,255,255);">break文で、switch文から抜けます</td> </tr><tr><td style="background-color:rgb(192,192,192);">        case 2:</td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">            print  &quot;$aの値は2です&quot;;</td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">            break;</td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">        case 3:</td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">            print  &quot;$aの値は2です&quot;;</td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">            break;</td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">        default:</td> <td style="background-color:rgb(204,255,255);"> case文に当てはまらない場合、全てdefaultで処理されます</td> </tr><tr><td style="background-color:rgb(192,192,192);">            print  &quot;$aの値は1~3以外です&quot;;</td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">            break;</td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">    }</td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">?&gt;</td> <td style="background-color:rgb(204,255,255);"> </td> </tr></tbody></table><p> </p> <h4 style="background-color:rgb(255,153,204);">for文</h4> <table cellspacing="1" cellpadding="1" border="0" width="606"><tbody><tr><td style="background-color:rgb(192,192,192);">&lt;?php</td> <td style="background-color:rgb(204,255,255);">▼▼▼説明▼▼▼</td> </tr><tr><td style="background-color:rgb(192,192,192);">    $a = 2;</td> <td style="background-color:rgb(204,255,255);">変数$aに2を代入</td> </tr><tr><td style="background-color:rgb(192,192,192);">   </td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">    for ($cnt = 1; $cnt &lt;= 10; $cnt++) {</td> <td style="background-color:rgb(204,255,255);">ループ処理を1から始め10で終了</td> </tr><tr><td style="background-color:rgb(192,192,192);">        print  $cnt . &quot;回目の変数$aは&quot; . $a .&quot;&lt;br /&gt;&quot;;</td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">        $a += 5;</td> <td style="background-color:rgb(204,255,255);">変数$aにプラス5を代入</td> </tr><tr><td style="background-color:rgb(192,192,192);">    }</td> <td style="background-color:rgb(204,255,255);">変数$cntにプラス1を代入($cnt++)部分</td> </tr><tr><td style="background-color:rgb(192,192,192);">    print  &quot;ループ終了&quot;;</td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">?&gt;</td> <td style="background-color:rgb(204,255,255);"> </td> </tr></tbody></table><h4 style="background-color:rgb(255,153,204);">while文</h4> <table cellspacing="1" cellpadding="1" border="0" width="601"><tbody><tr><td style="background-color:rgb(192,192,192);">&lt;?php</td> <td style="background-color:rgb(204,255,255);">▼▼▼説明▼▼▼</td> </tr><tr><td style="background-color:rgb(192,192,192);">    $a = 2;</td> <td style="background-color:rgb(204,255,255);">変数$aに2を代入</td> </tr><tr><td style="background-color:rgb(192,192,192);">    $cnt = 1;</td> <td style="background-color:rgb(204,255,255);">変数$cntに1を代入</td> </tr><tr><td style="background-color:rgb(192,192,192);"> </td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">    while ($cnt &lt;= 10) {</td> <td style="background-color:rgb(204,255,255);">while文で$cntが10になるまでループ処理</td> </tr><tr><td style="background-color:rgb(192,192,192);">         print  $cnt . &quot;回目の変数$aの値は&quot; . $a . &quot;です&lt;br /&gt;&quot;;</td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">         $a +=2;</td> <td style="background-color:rgb(204,255,255);">変数$aにプラス2を代入</td> </tr><tr><td style="background-color:rgb(192,192,192);">         $cnt++;</td> <td style="background-color:rgb(204,255,255);">変数$cntにプラス1を代入</td> </tr><tr><td style="background-color:rgb(192,192,192);">    }</td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">    print  &quot;ループ終了&quot;;</td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">?&gt;</td> <td style="background-color:rgb(204,255,255);"> </td> </tr></tbody></table><p> </p> <h4 style="background-color:rgb(255,153,204);">do ~ while文</h4> <p> </p> <table cellspacing="1" cellpadding="1" border="0" width="603"><tbody><tr><td style="background-color:rgb(192,192,192);">&lt;?php</td> <td style="background-color:rgb(204,255,255);">▼▼▼説明▼▼▼</td> </tr><tr><td style="background-color:rgb(192,192,192);">    $a = 2;</td> <td style="background-color:rgb(204,255,255);">変数$aに2を代入</td> </tr><tr><td style="background-color:rgb(192,192,192);">    $cnt = 10;</td> <td style="background-color:rgb(204,255,255);">変数$cntに10を代入</td> </tr><tr><td style="background-color:rgb(192,192,192);"> </td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">    do {</td> <td style="background-color:rgb(204,255,255);">do文にて処理</td> </tr><tr><td style="background-color:rgb(192,192,192);">        print  $cnt . &quot;回目の$aの値は&quot; . $a . &quot;です&lt;br /&gt;&quot;;</td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">        $a += 2;</td> <td style="background-color:rgb(204,255,255);">$aにプラス2を代入</td> </tr><tr><td style="background-color:rgb(192,192,192);">        $cnt++</td> <td style="background-color:rgb(204,255,255);">$cntにプラス1を代入</td> </tr><tr><td style="background-color:rgb(192,192,192);">    } while ($cnt &lt;= 5);</td> <td style="background-color:rgb(204,255,255);">while文にて処理</td> </tr><tr><td style="background-color:rgb(192,192,192);">    print  &quot;ループ終了&quot;;</td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">?&gt;</td> <td style="background-color:rgb(204,255,255);"> </td> </tr></tbody></table><p> ◆ここで重要なのは、<font color="#FF0000">最低1回は処理される</font>という点です.do以下の処理が実行されて、while文にて条件処理されるので、$cntは10以上でfalseのはずが、<br /> 実行結果は<br /> 「10回目の$aの値は2です」と表示されます.</p> <p> </p> <h4 style="background-color:rgb(255,153,204);">foreach文</h4> <p> </p> <table cellspacing="1" cellpadding="1" border="0" width="639"><tbody><tr><td style="background-color:rgb(192,192,192);">&lt;?php</td> <td style="background-color:rgb(204,255,255);">▼▼▼説明▼▼▼</td> </tr><tr><td style="background-color:rgb(192,192,192);">    $goods =  array (</td> <td style="background-color:rgb(204,255,255);">変数$goodsに連想配列を代入</td> </tr><tr><td style="background-color:rgb(192,192,192);">                                    &quot;apple&quot; =&gt; &quot;りんご&quot;,</td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">                                    &quot;orange&quot; =&gt; &quot;オレンジ&quot;,</td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">                                    &quot;grape&quot; =&gt; &quot;ぶどう&quot;</td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">                      )</td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">   </td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">    foreach ($goods as $key =&gt; $value) {</td> <td style="background-color:rgb(204,255,255);">foreach文によるループ処理</td> </tr><tr><td style="background-color:rgb(192,192,192);">        print  $key . &quot;は、&quot; . $value . &quot;です&lt;br /&gt;&quot;;</td> <td style="background-color:rgb(204,255,255);"> ここでは、$keyが英単語、$valueが日本語になる</td> </tr><tr><td style="background-color:rgb(192,192,192);">    }</td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">?&gt;</td> <td style="background-color:rgb(204,255,255);"> </td> </tr></tbody></table><p> </p> <p> </p> <p> </p> <p> </p> <p> </p> <hr width="100%" size="2" /><hr width="100%" size="2" /><p> </p> <p> </p>
<h3>条件分岐</h3> <p> <font color="#000080" size="4" style="background-color:rgb(255,204,153);">算術演算子</font></p> <h4 style="background-color:rgb(255,153,204);">if文</h4> <table cellspacing="1" cellpadding="1" border="0" width="586"><tbody><tr><td style="background-color:rgb(192,192,192);">&lt;?php</td> <td style="background-color:rgb(204,255,255);">▼▼▼説明▼▼▼</td> </tr><tr><td style="background-color:rgb(192,192,192);">$a = 200;</td> <td style="background-color:rgb(204,255,255);"> 変数$aに200を代入</td> </tr><tr><td style="background-color:rgb(192,192,192);">if ($a &gt; 200) {</td> <td style="background-color:rgb(204,255,255);"> $aは200より大きいの?</td> </tr><tr><td style="background-color:rgb(192,192,192);">    print &quot;値は200より大きいです。&quot;;</td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">} elseif ($a &gt; 100) {</td> <td style="background-color:rgb(204,255,255);"> $aは100より大きいの?</td> </tr><tr><td style="background-color:rgb(192,192,192);">    print &quot;値は200以下で100より大きいです。&quot;;</td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">} else {</td> <td style="background-color:rgb(204,255,255);"> $aは上記以外なの?</td> </tr><tr><td style="background-color:rgb(192,192,192);">    print &quot;値は100以下です。&quot;;</td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">}</td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">?&gt;</td> <td style="background-color:rgb(204,255,255);"> </td> </tr></tbody></table><p> ◆三項演算子<br /><code><span style="color:rgb(0,0,0);"><span style="color:rgb(0,0,187);">$bool =</span><span style="color:rgb(0,119,0);">($a == 200</span><span style="color:rgb(0,119,0);">) ?</span><span style="color:rgb(221,0,0);">'200です'</span><span style="color:rgb(0,119,0);">:</span></span></code><code><span style="color:rgb(0,0,0);"><span style="color:rgb(221,0,0);">'200じゃないです'</span></span></code><code><span style="color:rgb(0,0,0);"><span style="color:rgb(0,119,0);">;</span></span></code><br /> これは、if文の簡単な書き方で、もし$aが200なら変数$boolに「200です」を代入し、それ以外は「200じゃないです」を代入する書き方です.</p> <h4 style="background-color:rgb(255,153,204);">比較演算子</h4> <table cellspacing="1" cellpadding="1" border="0" width="586"><tbody><tr><td style="background-color:rgb(192,192,192);">比較条件</td> <td style="background-color:rgb(192,192,192);">比較演算子</td> <td style="background-color:rgb(192,192,192);">用例</td> </tr><tr><td>等しい</td> <td>  ==</td> <td>  $a == $b(aとbの値が等しいならtrue)</td> </tr><tr><td>~以上</td> <td>  &gt;=</td> <td>  $a &gt;= $b(aの値がb以上ならtrue)</td> </tr><tr><td>~より大きい</td> <td>  &gt;</td> <td>  $a &gt; $b(aの値がbより大きいならtrue)</td> </tr><tr><td> ~以下</td> <td>  &lt;=</td> <td>  $a &lt;= $b(aの値がb以下ならtrue)</td> </tr><tr><td> より小さい</td> <td>  &lt;</td> <td>  $a &lt; $b(aの値がbより小さいならtrue)</td> </tr><tr><td> 等しくない</td> <td>  != または &lt;&gt;</td> <td>  $a != $b(aの値が異なればtrue)</td> </tr></tbody></table><p> </p> <h4 style="background-color:rgb(255,153,204);">論理演算子</h4> <p> </p> <table cellspacing="1" cellpadding="1" border="0" width="599"><tbody><tr><td style="background-color:rgb(192,192,192);">比較条件</td> <td style="background-color:rgb(192,192,192);">論理演算子</td> <td style="background-color:rgb(192,192,192);">比較の仕方</td> </tr><tr><td>論理積</td> <td> and または &amp;&amp;</td> <td>条件式がすべてtrueなら、全体としてtrue</td> </tr><tr><td>論理和</td> <td> or または ||</td> <td>条件式の内1つがtrueなら、全体としてtrue</td> </tr><tr><td>否定</td> <td> !</td> <td>【 ! 】の次の条件式がfalseなら、全体としてtrue</td> </tr><tr><td>排他的論理和</td> <td> xor</td> <td>前後条件式の一方がtrueでもう一方がfalseなら、全体としてtrue</td> </tr></tbody></table><p> <font color="#FF0000">サンプル</font></p> <table cellspacing="1" cellpadding="1" border="0" width="592"><tbody><tr><td style="background-color:rgb(192,192,192);">&lt;?php</td> <td style="background-color:rgb(204,255,255);"> ▼▼▼説明▼▼▼</td> </tr><tr><td style="background-color:rgb(192,192,192);">    $a = 123;</td> <td style="background-color:rgb(204,255,255);"> 変数$aに123を代入</td> </tr><tr><td style="background-color:rgb(192,192,192);">    $b = 101;</td> <td style="background-color:rgb(204,255,255);"> 変数$bに101を代入</td> </tr><tr><td style="background-color:rgb(192,192,192);"> </td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">    if ($a &gt; 100<font color="#FF0000">and</font>$a &gt; $b) {</td> <td style="background-color:rgb(204,255,255);"> 二つの式がtrueなら、全体としてtrue</td> </tr><tr><td style="background-color:rgb(192,192,192);">        print  &quot;$aは100より大きくかつ$bより大きい&quot;;</td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">    }</td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">    if ($a &gt; 100<font color="#FF0000">or</font>$b &gt; 100) {</td> <td style="background-color:rgb(204,255,255);">  二つの式の一つがtrueなら、全体としてtrue</td> </tr><tr><td style="background-color:rgb(192,192,192);">        print  &quot;$aと$bのいずれかは100より大きい&quot;;</td> <td style="background-color:rgb(204,255,255);">  二つの式ともtrueの場合でも、全体としてtrue</td> </tr><tr><td style="background-color:rgb(192,192,192);">    }</td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">    if (<font color="#FF0000">!</font>($a == 100)) {</td> <td style="background-color:rgb(204,255,255);"> $aが100<font color="#FF0000">ではないなら</font>、全体としてtrue</td> </tr><tr><td style="background-color:rgb(192,192,192);">        print  &quot;$aは100ではない&quot;;</td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">    }</td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">?&gt;</td> <td style="background-color:rgb(204,255,255);"> </td> </tr></tbody></table><p> </p> <h4 style="background-color:rgb(255,153,204);">switch文</h4> <p> </p> <table cellspacing="1" cellpadding="1" border="0" width="605"><tbody><tr><td style="background-color:rgb(192,192,192);">&lt;?php</td> <td style="background-color:rgb(204,255,255);">▼▼▼説明▼▼▼</td> </tr><tr><td style="background-color:rgb(192,192,192);">    $a = 2;</td> <td style="background-color:rgb(204,255,255);">変数$aに2を代入</td> </tr><tr><td style="background-color:rgb(192,192,192);"> </td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">    switch ($a) {</td> <td style="background-color:rgb(204,255,255);">switch文のより$aの値を比較</td> </tr><tr><td style="background-color:rgb(192,192,192);">        case 1:</td> <td style="background-color:rgb(204,255,255);">case 文の末尾は【 : 】(コロン)</td> </tr><tr><td style="background-color:rgb(192,192,192);">            print  &quot;$aの値は1です&quot;;</td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">            break;</td> <td style="background-color:rgb(204,255,255);">break文で、switch文から抜けます</td> </tr><tr><td style="background-color:rgb(192,192,192);">        case 2:</td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">            print  &quot;$aの値は2です&quot;;</td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">            break;</td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">        case 3:</td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">            print  &quot;$aの値は2です&quot;;</td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">            break;</td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">        default:</td> <td style="background-color:rgb(204,255,255);"> case文に当てはまらない場合、全てdefaultで処理されます</td> </tr><tr><td style="background-color:rgb(192,192,192);">            print  &quot;$aの値は1~3以外です&quot;;</td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">            break;</td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">    }</td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">?&gt;</td> <td style="background-color:rgb(204,255,255);"> </td> </tr></tbody></table><p> </p> <h4 style="background-color:rgb(255,153,204);">for文</h4> <table cellspacing="1" cellpadding="1" border="0" width="606"><tbody><tr><td style="background-color:rgb(192,192,192);">&lt;?php</td> <td style="background-color:rgb(204,255,255);">▼▼▼説明▼▼▼</td> </tr><tr><td style="background-color:rgb(192,192,192);">    $a = 2;</td> <td style="background-color:rgb(204,255,255);">変数$aに2を代入</td> </tr><tr><td style="background-color:rgb(192,192,192);">   </td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">    for ($cnt = 1; $cnt &lt;= 10; $cnt++) {</td> <td style="background-color:rgb(204,255,255);">ループ処理を1から始め10で終了</td> </tr><tr><td style="background-color:rgb(192,192,192);">        print  $cnt . &quot;回目の変数$aは&quot; . $a .&quot;&lt;br /&gt;&quot;;</td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">        $a += 5;</td> <td style="background-color:rgb(204,255,255);">変数$aにプラス5を代入</td> </tr><tr><td style="background-color:rgb(192,192,192);">    }</td> <td style="background-color:rgb(204,255,255);">変数$cntにプラス1を代入($cnt++)部分</td> </tr><tr><td style="background-color:rgb(192,192,192);">    print  &quot;ループ終了&quot;;</td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">?&gt;</td> <td style="background-color:rgb(204,255,255);"> </td> </tr></tbody></table><h4 style="background-color:rgb(255,153,204);">while文</h4> <table cellspacing="1" cellpadding="1" border="0" width="601"><tbody><tr><td style="background-color:rgb(192,192,192);">&lt;?php</td> <td style="background-color:rgb(204,255,255);">▼▼▼説明▼▼▼</td> </tr><tr><td style="background-color:rgb(192,192,192);">    $a = 2;</td> <td style="background-color:rgb(204,255,255);">変数$aに2を代入</td> </tr><tr><td style="background-color:rgb(192,192,192);">    $cnt = 1;</td> <td style="background-color:rgb(204,255,255);">変数$cntに1を代入</td> </tr><tr><td style="background-color:rgb(192,192,192);"> </td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">    while ($cnt &lt;= 10) {</td> <td style="background-color:rgb(204,255,255);">while文で$cntが10になるまでループ処理</td> </tr><tr><td style="background-color:rgb(192,192,192);">         print  $cnt . &quot;回目の変数$aの値は&quot; . $a . &quot;です&lt;br /&gt;&quot;;</td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">         $a +=2;</td> <td style="background-color:rgb(204,255,255);">変数$aにプラス2を代入</td> </tr><tr><td style="background-color:rgb(192,192,192);">         $cnt++;</td> <td style="background-color:rgb(204,255,255);">変数$cntにプラス1を代入</td> </tr><tr><td style="background-color:rgb(192,192,192);">    }</td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">    print  &quot;ループ終了&quot;;</td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">?&gt;</td> <td style="background-color:rgb(204,255,255);"> </td> </tr></tbody></table><p> </p> <h4 style="background-color:rgb(255,153,204);">do ~ while文</h4> <p> </p> <table cellspacing="1" cellpadding="1" border="0" width="603"><tbody><tr><td style="background-color:rgb(192,192,192);">&lt;?php</td> <td style="background-color:rgb(204,255,255);">▼▼▼説明▼▼▼</td> </tr><tr><td style="background-color:rgb(192,192,192);">    $a = 2;</td> <td style="background-color:rgb(204,255,255);">変数$aに2を代入</td> </tr><tr><td style="background-color:rgb(192,192,192);">    $cnt = 10;</td> <td style="background-color:rgb(204,255,255);">変数$cntに10を代入</td> </tr><tr><td style="background-color:rgb(192,192,192);"> </td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">    do {</td> <td style="background-color:rgb(204,255,255);">do文にて処理</td> </tr><tr><td style="background-color:rgb(192,192,192);">        print  $cnt . &quot;回目の$aの値は&quot; . $a . &quot;です&lt;br /&gt;&quot;;</td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">        $a += 2;</td> <td style="background-color:rgb(204,255,255);">$aにプラス2を代入</td> </tr><tr><td style="background-color:rgb(192,192,192);">        $cnt++</td> <td style="background-color:rgb(204,255,255);">$cntにプラス1を代入</td> </tr><tr><td style="background-color:rgb(192,192,192);">    } while ($cnt &lt;= 5);</td> <td style="background-color:rgb(204,255,255);">while文にて処理</td> </tr><tr><td style="background-color:rgb(192,192,192);">    print  &quot;ループ終了&quot;;</td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">?&gt;</td> <td style="background-color:rgb(204,255,255);"> </td> </tr></tbody></table><p> ◆ここで重要なのは、<font color="#FF0000">最低1回は処理される</font>という点です.do以下の処理が実行されて、while文にて条件処理されるので、$cntは10以上でfalseのはずが、<br /> 実行結果は<br /> 「10回目の$aの値は2です」と表示されます.</p> <p> </p> <h4 style="background-color:rgb(255,153,204);">foreach文</h4> <table cellspacing="1" cellpadding="1" border="0" width="639"><tbody><tr><td style="background-color:rgb(192,192,192);">&lt;?php</td> <td style="background-color:rgb(204,255,255);">▼▼▼説明▼▼▼</td> </tr><tr><td style="background-color:rgb(192,192,192);">    $goods =  array (</td> <td style="background-color:rgb(204,255,255);">変数$goodsに連想配列を代入</td> </tr><tr><td style="background-color:rgb(192,192,192);">                                    &quot;apple&quot; =&gt; &quot;りんご&quot;,</td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">                                    &quot;orange&quot; =&gt; &quot;オレンジ&quot;,</td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">                                    &quot;grape&quot; =&gt; &quot;ぶどう&quot;</td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">                      )</td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">   </td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">    foreach ($goods as $key =&gt; $value) {</td> <td style="background-color:rgb(204,255,255);">foreach文によるループ処理</td> </tr><tr><td style="background-color:rgb(192,192,192);">        print  $key . &quot;は、&quot; . $value . &quot;です&lt;br /&gt;&quot;;</td> <td style="background-color:rgb(204,255,255);"> ここでは、$keyが英単語、$valueが日本語になる</td> </tr><tr><td style="background-color:rgb(192,192,192);">    }</td> <td style="background-color:rgb(204,255,255);"> </td> </tr><tr><td style="background-color:rgb(192,192,192);">?&gt;</td> <td style="background-color:rgb(204,255,255);"> </td> </tr></tbody></table><p>◆【 as 】の次に指定された「キー変数」にはキーの値、【 =&gt; 】の次に指定された「値変数」にはデータの値を代入します.</p> <p> </p> <p> </p> <p> </p> <p> </p> <hr width="100%" size="2" /><hr width="100%" size="2" /><p> </p> <p> </p>

表示オプション

横に並べて表示:
変化行の前後のみ表示:
記事メニュー
人気記事ランキング
目安箱バナー