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.
You can make multiple comparisons with else.
You can compare multiple conditions with “elseif”.
repetition
The “while” statement is repeated until a comparison value satisfies the condition.
The for statement declares the repeat condition with the () immediately after it.
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.
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>