PHP Control Flow Statement

PHP Control Flow Statement

這篇文章將介紹以下四種 Control Flow:

  • If…Else…Elseif
  • Switch
  • While Loops
  • For Loops

If…Else…Elseif

有下列這幾種 if statement:

  • if statement - executes some code if one condition is true
  • if…else statement - executes some code if a condition is true and another code if that condition is false
  • if…elseif….else statement - executes different codes for more than two conditions

Syntax - 1

1
2
3
4
5
6
7
8
9
10
11
12
13
if (condition) {
code to be executed if condition is true;
}
// Example
<?php
$t = 10;
if ($t < 20) {
echo "Have a good day!";
}
// 輸出結果為: Have a good day!
?>

Syntax - 2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
if (condition) {
code to be executed if condition is true;
} else {
code to be executed if condition is false;
}
// Example
<?php
$t = 22;
if ($t < 20) {
echo "Have a good day!";
} else {
echo "Have a good night!";
}
// 輸出結果為:Have a good night!
?>

Syntax - 3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
if (condition) {
code to be executed if this condition is true;
} elseif (condition) {
code to be executed if this condition is true;
} else {
code to be executed if all conditions are false;
}
// Example
<?php
$t = 5;
if ($t < 10) {
echo "Have a good morning!";
} elseif ($t < "20") {
echo "Have a good day!";
} else {
echo "Have a good night!";
}
// 輸出結果為:Have a good morning!
?>


Switch

個人認為是用於,已知有哪些條件下時的control flow。
break:是避免往下繼續跑另一區塊的code。
default:是所有case都不符合時會跑的區塊。

Syntax

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
switch (n) {
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
case label3:
code to be executed if n=label3;
break;
...
default:
code to be executed if n is different from all labels;
}
// Example
<?php
$favcolor = "red";
switch ($favcolor) {
case "red":
echo "Your favorite color is red!";
break;
case "blue":
echo "Your favorite color is blue!";
break;
case "green":
echo "Your favorite color is green!";
break;
default:
echo "Your favorite color is neither red, blue, nor green!";
}
// 輸出結果:Your favorite color is red!
?>


While Loops

當你需要一直重複執行一個code block 且 終止條件不確定在何時會達到下,此時就可以用 While Loops 來達到你的需求。
有以下兩種可以使用:

  • while - 一直重複執行直到條件不成立。
  • do…while - 至少執行一次再繼續執行到條件不成立為止。

Syntax - 1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
while (condition is true) {
code to be executed;
}
// Example
<?php
$x = 1;
while($x <= 5) {
echo "The number is: $x <br>";
$x++;
}
?>
// 輸出結果:
//The number is: 1
//The number is: 2
//The number is: 3
//The number is: 4
//The number is: 5

Syntax - 2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
do {
code to be executed;
} while (condition is true);
// Example
<?php
$x = 6;
do {
echo "The number is: $x <br>";
$x++;
} while ($x <= 5);
// 輸出結果:The number is: 6
?>


For Loops

for loop適用於已知特定次數的情況下,來執行相同的code block。
有以下兩種類型:

  • for loop - 可設置 初始條件、結束條件跟遞增條件
  • foreach - 專門給 array 使用。

Syntax - 1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
for (init counter; test counter; increment counter) {
code to be executed;
}
// Example
<?php
for ($x = 0; $x <= 10; $x++) {
echo "The number is: $x <br>";
}
/*
輸出結果:
The number is: 0
The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5
The number is: 6
The number is: 7
The number is: 8
The number is: 9
The number is: 10
*/
?>

Syntax - 2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
foreach ($array as $value) {
code to be executed;
}
// Example
<?php
$colors = array("red", "green", "blue", "yellow");
foreach ($colors as $value) {
echo "$value <br>";
}
/*
輸出結果:
red
green
blue
yellow
*/
?>