PHP Part03 Control Statements

php004

Let’s learn while actually coding control statements in PHP.

There are many control statements, but we have picked up the ones that are commonly used.

If you haven’t done so last time, please refer to the following.

Control statement

If true / false is satisfied in the conditional expression, the process is executed.

if
/* if */
$temp = 100;
//Display A if greater than 100
if($temp > 99){
    print("A");
    print("<br/>");
}

結果

ABC

You can make multiple comparisons with else.

else
/* else */
$temp = 100;
// If it is less than 99, A is displayed, otherwise B is displayed.
if($temp < 99){
    print("A");
    print("<br/>");
}else{
    print("B");
    print("<br/>");
}

結果

B

You can compare multiple conditions with “elseif”.

elseif
/* elseif */
$temp = 100;
// Display A if less than 99
if($temp < 99){
    print("A");
    print("<br/>");
// If it is larger than 99, C is displayed, otherwise B is displayed.
}elseif($temp > 99){
    print("C");
    print("<br/>");
}else{
    print("B");
    print("<br/>");
}

結果

B

repetition

The “while” statement is repeated until a comparison value satisfies the condition.

while
/* while */
$temp = 0;
// Repeat while temp is less than 10
while($temp < 10){
    print($temp);
    print("<br/>");
    $temp++;
}

結果

0
1
2
3
4
5
6
7
8
9

The for statement declares the repeat condition with the () immediately after it.

for
/* for */
// Repeat while temp is less than 10
for($temp = 0; $temp < 10; $temp++){
    print($temp);
    print("<br/>");
}

結果

0
1
2
3
4
5
6
7
8
9

switch

The switch statement executes case processing that matches the condition value.

The break statement is used to end the process and exit the control statement.

defalt is executed when none of the conditions are met.

switch
/* switch */
$temp = "BBB";
// Cases that match temp are processed
switch($temp){
    case "AAA":
        print("A");
        break;
        print("<br/>");
    case "BBB":
        print("B");
        print("<br/>");
        break;
    case "CCC":
        print("C");
        print("<br/>");
        break;
    defalt:
        print("Unknown");
        print("<br/>");
        break;
}

結果

B

that’s all. good job for today.

<html>
 <head>
  <title>PHP</title>
 </head>
 <body>
 <?php
    /* if */
    $temp = 100;
    // 100より大きい場合はAを表示する
    if($temp > 99){
        print("A");
        print("<br/>"); // 改行
    }

    /* else */
    $temp = 100;
    // 99より小さい場合はAを表示する,それ以外はBを表示する
    if($temp < 99){
        print("A");
        print("<br/>"); // 改行
    }else{
        print("B");
        print("<br/>"); // 改行
    }

    /* elseif */
    $temp = 100;
    // 99より小さい場合はAを表示する
    if($temp < 99){
        print("A");
        print("<br/>"); // 改行
    // 99より大きい場合はCを表示する,それ以外はBを表示する
    }elseif($temp > 99){
        print("C");
        print("<br/>"); // 改行
    }else{
        print("B");
        print("<br/>"); // 改行
    }

    /* while */
    $temp = 0;
    // tempが10より小さい間繰り返す
    while($temp < 10){
        print($temp);
        print("<br/>"); // 改行
        $temp++;
    }

    /* for */
    // tempが10より小さい間繰り返す
    for($temp = 0; $temp < 10; $temp++){
        print($temp);
        print("<br/>"); // 改行
    }

    /* switch */
    $temp = "BBB";
    // tempと一致するcaseが処理される
    switch($temp){
        case "AAA":
            print("A");
            break;
            print("<br/>"); // 改行
        case "BBB":
            print("B");
            print("<br/>"); // 改行
            break;
        case "CCC":
            print("C");
            print("<br/>"); // 改行
            break;
        defalt:
            print("Unknown");
            print("<br/>"); // 改行
            break;
    }
 ?>
 </body>
</html>
タイトルとURLをコピーしました