Blogger Information
Blog 21
fans 0
comment 1
visits 19077
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP三大流程控制
XFY_肆意De...
Original
829 people have browsed it
顺序流程控制
  1. $a1 = '这是第1条语句<br>';
  2. $a2 = '这是第2条语句<br>';
  3. $a3 = '这是第3条语句<br>';
  4. $a4 = '这是第4条语句<br>';
  5. $a5 = '这是第5条语句<br>';
  6. echo $a1, $a2, $a3, $a4, $a5;
分支流程控制
  1. 单分支流程控制
  2. 分支控制,就是代码中多了判断,根据真假控制执行语句;
  3. $a6等于0false):按相反顺序输出$a5~$a1
  1. $a1 = '这是第1条语句<br>';
  2. $a2 = '这是第2条语句<br>';
  3. $a3 = '这是第3条语句<br>';
  4. $a4 = '这是第4条语句<br>';
  5. $a5 = '这是第5条语句<br>';
  6. $a6 = 0;
  7. if($a6){
  8. echo $a5, $a4, $a3, $a2, $a1;
  9. }
双分支流程控制
  1. /*
  2. 当$a6等于1(true):按顺序输出$a1~$a5
  3. 当$a6等于0(false):按相反顺序输出$a5~$a1
  4. */
  5. $a1 = '这是第1条语句<br>';
  6. $a2 = '这是第2条语句<br>';
  7. $a3 = '这是第3条语句<br>';
  8. $a4 = '这是第4条语句<br>';
  9. $a5 = '这是第5条语句<br>';
  10. $a6 = 0;
  11. if($a6){
  12. echo $a1, $a2, $a3, $a4, $a5;
  13. }else{
  14. echo $a5, $a4, $a3, $a2, $a1;
  15. }
多分支流程控制
  1. $a6等于1:输出$a1
  2. $a6等于2:输出$a2
  3. $a6等于3:输出$a3
  4. $a6等于4:输出$a4
  5. $a6等于5:输出$a5
  6. 否则:按顺序输出$a1~$a5

if(表达式)

  1. $a1 = '这是第1条语句<br>';
  2. $a2 = '这是第2条语句<br>';
  3. $a3 = '这是第3条语句<br>';
  4. $a4 = '这是第4条语句<br>';
  5. $a5 = '这是第5条语句<br>';
  6. $a6 = 5;
  7. if ($a6 === 1) {
  8. echo $a1;
  9. } elseif ($a6 === 2) {
  10. echo $a2;
  11. } elseif ($a6 === 3) {
  12. echo $a3;
  13. } elseif ($a6 === 4) {
  14. echo $a4;
  15. } elseif ($a6 === 5) {
  16. echo $a5;
  17. } else {
  18. echo $a1, $a2, $a3, $a4, $a5;
  19. }

switch(表达式)-简化分支

  1. switch ($a6) {
  2. case $a6 === 1;
  3. echo $a1;
  4. break;
  5. case $a6 === 2;
  6. echo $a2;
  7. break;
  8. case $a6 === 3;
  9. echo $a3;
  10. break;
  11. case $a6 === 4;
  12. echo $a4;
  13. break;
  14. case $a6 === 5;
  15. echo $a5;
  16. break;
  17. default:
  18. echo $a1, $a2, $a3, $a4, $a5;
  19. }
循环控制

学生表成绩如下:

  1. //学生成绩表
  2. $student = [
  3. ['id' => 1, 'name' => '小明', 'grade' => 80],
  4. ['id' => 2, 'name' => '张三', 'grade' => 97],
  5. ['id' => 3, 'name' => '小丽', 'grade' => 95],
  6. ['id' => 4, 'name' => '小王', 'grade' => 78],
  7. ['id' => 5, 'name' => '李四', 'grade' => 98],
  8. ['id' => 6, 'name' => '小美', 'grade' => 88]
  9. ];

使用不同循环输出全部信息:
while 循环

  1. while ($students = current($student)) {
  2. echo $students['id'] . '.' . $students['name'] . '的成绩是:' . $students['grade'] . '<br>';
  3. next($student);
  4. }

do...while 循环

  1. $num = 0;
  2. do {
  3. echo $student[$num]['id'] . '.' . $student[$num]['name'] . '的成绩是:' . $student[$num]['grade'] . '<br>';
  4. $num++;
  5. } while ($num < count($student));

for 循环

  1. for ($num = 0; $num < count($student); $num++) {
  2. echo $student[$num]['id'] . '.' . $student[$num]['name'] . '的成绩是:' . $student[$num]['grade'] . '<br>';
  3. }

以上3种方法均输出同样结过:

  1. 1.小明的成绩是:80
  2. 2.张三的成绩是:97
  3. 3.小丽的成绩是:95
  4. 4.小王的成绩是:78
  5. 5.李四的成绩是:98
  6. 6.小美的成绩是:88

总结:

  1. PHP三大流程控制有了进一步了解

知识点

  1. <?php
  2. // 1、当流程控制语句中只有一行代码的时候,可以省略大括号
  3. if ($a > $b) echo 'a大于b';
  4. else echo 'a小于b';
  5. // 2、三元运算符语法:条件 ? 结果(true) : 结果(false)
  6. echo ($a > $b) ? 'a大于b' : 'a小于b';
  7. // 3、模板语法(将起始大括号用冒号代替,将结束大括号用特殊关键字代替)
  8. // if语句:
  9. if ($a > $b) : echo 'a大于b';
  10. else : echo 'a小于b';
  11. endif;
  12. // while语句:
  13. while ($students = current($student)) : echo $students['id'] . '.' . $students['name'] . '的成绩是:' . $students['grade'] . '<br>';
  14. next($student);
  15. endwhile;
Correcting teacher:GuanhuiGuanhui

Correction status:qualified

Teacher's comments:代码写的挺好,就是没有文字描述一下
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post