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:
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: