第 6 章 PHP 的流程控制功能

第 6 章 PHP 的流程控制功能

6.1 转换流程

6.1.1 if 语句

<?php
$mood = "happy";
if ($mood == "happy") {
    echo "Hooray! I'm in a good mood!";
}
?>

结果:

Hooray! I'm in a good mood!

6.1.2 使用 else 子句的 if 语句

<?php
$mood = "sad";
if ($mood == "happy") {
    echo "Hooray! I'm in a good mood!";
} else {
    echo "I'm in a $mood mood.";
}
?>

结果:

I'm in a sad mood.

6.1.3 使用带有 elseif 子句的 if 语句

<?php
$mood = "sad";
if ($mood == "happy") {
    echo "Hooray! I'm in a good mood!";
} elseif ($mood == "sad") {
    echo "Awww. Don't be down!";
} else {
    echo "I'm neither happy nor sad, but $mood.";
}
?>

结果:

I'm in a sad mood.

6.1.4 switch 语句

<?php
$mood = "sad";
switch ($mood) {
    case "happy":
        echo "Hooray! I'm in a good mood!";
        break;
    case "sad":
        echo "Awww. Don't be down!";
        break;
    default:
        echo "I'm neither happy nor sad, but $mood.";
        break;
}
?>

结果:

Awww. Don't be down!

6.1.5 使用 ? 运算符

<?php
$mood = "sad";
$text = ($mood == "happy") ? "I am in a good mood!" : "I am in a $mood mood.";
echo "$text";
?>

结果:

I am in a sad mood.

6.2 循环

6.2.1 while 语句

<?php
$counter = 1;
while ($counter <= 12) {
    echo $counter." times 2 is ".($counter * 2)."<br/>";
    $counter++;
}
?>

结果:

1 times 2 is 2
2 times 2 is 4
3 times 2 is 6
4 times 2 is 8
5 times 2 is 10
6 times 2 is 12
7 times 2 is 14
8 times 2 is 16
9 times 2 is 18
10 times 2 is 20
11 times 2 is 22
12 times 2 is 24

6.2.2 do...while 语句

<?php
$num = 395;
do {
    echo "The number is: ".$num."<br/>";
    $num++;
} while ( ($num > 200) && ($num < 400) );
?>

结果:

The number is: 395
The number is: 396
The number is: 397
The number is: 398
The number is: 399

6.2.3 for 语句

<?php
for ($counter = 1; $counter <= 12; $counter++) { 
    echo $counter." times 2 is ".($counter * 2)."<br/>";
}
?>

结果:

1 times 2 is 2
2 times 2 is 4
3 times 2 is 6
4 times 2 is 8
5 times 2 is 10
6 times 2 is 12
7 times 2 is 14
8 times 2 is 16
9 times 2 is 18
10 times 2 is 20
11 times 2 is 22
12 times 2 is 24

6.2.4 用 break 语句跳出循环

<?php
for ($counter = -4; $counter <= 10; $counter++) { 
    if ($counter == 0) {
        break;
    }
    $temp = 4000/$counter;
    echo "4000 divided by ".$counter." is $temp<br/>";
}
?>

结果:

4000 divided by -4 is -1000
4000 divided by -3 is -1333.33333333
4000 divided by -2 is -2000
4000 divided by -1 is -4000

提示:在 PHP 里用一个数除以零不会导致一个致命的错误。相反,PHP 产生一个警告并继续执行。

<?php
$result = 10/0;
echo "$result";
?>

结果:

<br />
<b>Warning</b>: Division by zero in <b>/Applications/MAMP/htdocs/example.php</b> on line <b>2</b><br />

6.2.5 用 continue 语句跳过迭代

程序清单 6.11 使用 continue 语句

<?php
for ($counter = -4; $counter <= 10; $counter++) { 
    if ($counter == 0) {
        continue;
    }
    $temp = 4000/$counter;
    echo "4000 divided by ".$counter." is $temp<br/>";
}
?>

结果:

4000 divided by -4 is -1000
4000 divided by -3 is -1333.33333333
4000 divided by -2 is -2000
4000 divided by -1 is -4000
4000 divided by 1 is 4000
4000 divided by 2 is 2000
4000 divided by 3 is 1333.33333333
4000 divided by 4 is 1000
4000 divided by 5 is 800
4000 divided by 6 is 666.666666667
4000 divided by 7 is 571.428571429
4000 divided by 8 is 500
4000 divided by 9 is 444.444444444
4000 divided by 10 is 400

6.2.6 嵌套循环

<?php
echo "<table style=\"border: 1px solid #000\">\n";
for ($y = 1; $y <= 12; $y++) {
    echo "<tr>\n";
    for ($x = 1; $x <= 12; $x++) {
        echo "<td style=\"border: 1px solid #000; width: 75px; text-align: center;\">";
        echo ($y * $x);
        echo "</td>\n";
    }
    echo "</tr>\n\n";
}
echo "</table>";
?>

结果:

<table style="border: 1px solid #000">
<tr>
<td style="border: 1px solid #000; width: 75px; text-align: center;">1</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">2</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">3</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">4</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">5</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">6</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">7</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">8</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">9</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">10</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">11</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">12</td>
</tr>

<tr>
<td style="border: 1px solid #000; width: 75px; text-align: center;">2</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">4</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">6</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">8</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">10</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">12</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">14</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">16</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">18</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">20</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">22</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">24</td>
</tr>

<tr>
<td style="border: 1px solid #000; width: 75px; text-align: center;">3</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">6</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">9</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">12</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">15</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">18</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">21</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">24</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">27</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">30</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">33</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">36</td>
</tr>

<tr>
<td style="border: 1px solid #000; width: 75px; text-align: center;">4</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">8</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">12</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">16</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">20</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">24</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">28</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">32</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">36</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">40</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">44</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">48</td>
</tr>

<tr>
<td style="border: 1px solid #000; width: 75px; text-align: center;">5</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">10</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">15</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">20</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">25</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">30</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">35</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">40</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">45</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">50</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">55</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">60</td>
</tr>

<tr>
<td style="border: 1px solid #000; width: 75px; text-align: center;">6</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">12</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">18</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">24</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">30</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">36</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">42</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">48</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">54</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">60</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">66</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">72</td>
</tr>

<tr>
<td style="border: 1px solid #000; width: 75px; text-align: center;">7</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">14</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">21</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">28</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">35</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">42</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">49</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">56</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">63</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">70</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">77</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">84</td>
</tr>

<tr>
<td style="border: 1px solid #000; width: 75px; text-align: center;">8</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">16</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">24</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">32</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">40</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">48</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">56</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">64</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">72</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">80</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">88</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">96</td>
</tr>

<tr>
<td style="border: 1px solid #000; width: 75px; text-align: center;">9</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">18</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">27</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">36</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">45</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">54</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">63</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">72</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">81</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">90</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">99</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">108</td>
</tr>

<tr>
<td style="border: 1px solid #000; width: 75px; text-align: center;">10</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">20</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">30</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">40</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">50</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">60</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">70</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">80</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">90</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">100</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">110</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">120</td>
</tr>

<tr>
<td style="border: 1px solid #000; width: 75px; text-align: center;">11</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">22</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">33</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">44</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">55</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">66</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">77</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">88</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">99</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">110</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">121</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">132</td>
</tr>

<tr>
<td style="border: 1px solid #000; width: 75px; text-align: center;">12</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">24</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">36</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">48</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">60</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">72</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">84</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">96</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">108</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">120</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">132</td>
<td style="border: 1px solid #000; width: 75px; text-align: center;">144</td>
</tr>

</table>

6.3 代码块和浏览器输出

<?php
$display_prices = true;
if ($display_prices) {
?>
<table border="1">
    <tr>
        <td colspan="3">today's prices in dollars</td>
    </tr>
    <tr>
        <td>$14.00</td>
        <td>$32.00</td>
        <td>$71.00</td>
    </tr>
</table>
<?php
}
?>

结果:

<table border="1">
<tr>
<td colspan="3">today's prices in dollars</td>
</tr>
<tr>
<td>$14.00</td>
<td>$32.00</td>
<td>$71.00</td>
</tr>
</table>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,362评论 5 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,330评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,247评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,560评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,580评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,569评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,929评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,587评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,840评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,596评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,678评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,366评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,945评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,929评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,165评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 43,271评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,403评论 2 342

推荐阅读更多精彩内容