Nested if...els...LOGIN

Nested if...else...elseif structure of PHP process control

I still remember that we talked about an example of classmate Wang Sixong at the beginning of this chapter:

Wang is a person whose life is extremely full of entertainment and enjoyment. What he did when he arrived in Beijing or Dalian, what he did after arriving, is as follows:

Arrived in the middle of the night, first went to a nightclub to attend a masquerade party
Arrive in the morning and take a bath in the hotel
Arrive at noon and have a portion of Kobe beef
Arriving at night, I always like to go to friends to talk about the loneliness in my heart

Let’s learn about his grammar rules [Knowledge point requirements: silent writing]

<?php
if(判断语句1){
    执行语句体1
}elseif(判断语句2){
    执行语句体2
}else if(判断语句n){
        执行语句体n
}else{
        最后的else语句可选
}

//后续代码
?>

The above structure means:
If the value of statement 1 is true, execute statement body 1. After execution is completed, enter the subsequent code segment.
Otherwise, go to the following judgment statement 2 (elsif). If judgment statement 2 is true, statement body 2 will be executed.
Otherwise, go to the following judgment statement n (elsif). If judgment statement 2 is true, execute statement body n.
If none match, execute the else statement. This kind of loop nesting can not contain else statements, that is, it can only contain if and elseif statements.

Note: elseif() can also be written as else if()

We express the above code clearly in the form of a flow chart as shown below:
2015-08-08/55c5966bd7af5

We can express the example of Mr. Wang Sixong through PHP code. The result of the code representation is as follows:

<?php
//定义一个随机变量,抵达时间,随机0点至23点
$dida = rand(0,23);

if($dida > 6 && $dida < 10){
    echo '我爱泡澡';
}else if($dida >10 && $dida < 14){
    echo '吃神户牛肉';
}else if($dida >=19 && $dida < 22){
    echo '找一个朋友聊聊内心的寂寞';
}elseif($dida > 22 && $dida <= 23){
    echo '泡澡';

}elseif($dida >= 1 && $dida <3){
     echo '泡澡';
}else{
    echo '睡觉或者工作';
}


?>

Assignment:
Write a web page cj.html , submit the score segment to the panduan.php page in post mode. The score range and displayed results are as follows, and the requirements are as follows:

  1. 0----Below 60, failed
  2. 60---70 passed, please work hard
  3. 70---80 Not bad
  4. 80---90 There is hope to go to Tsinghua University
  5. 90---100 You have no hope in this life
  6. 100 Even less I hope
  7. 100 points or more Einstein is reincarnated, Smecta!
  8. is not a numeric type or less than 0. Please enter the correct score.
Next Section
<?php //定义一个随机变量,抵达时间,随机0点至23点 $dida = rand(0,23); if($dida > 6 && $dida < 10){ echo '我爱泡澡'; }else if($dida >10 && $dida < 14){ echo '吃神户牛肉'; }else if($dida >=19 && $dida < 22){ echo '找一个朋友聊聊内心的寂寞'; }elseif($dida > 22 && $dida <= 23){ echo '泡澡'; }elseif($dida >= 1 && $dida <3){ echo '泡澡'; }else{ echo '睡觉或者工作'; } ?>
submitReset Code
ChapterCourseware