Blogger Information
Blog 36
fans 1
comment 0
visits 26408
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
实例演示分支与循环 流程控制
早晨
Original
362 people have browsed it

分支与循环演示代码

  1. <?php
  2. namespace _0809;
  3. $score = 70;
  4. // 单分支
  5. echo '-----------------------单分支-----------------------<br>';
  6. if ($score > 60) {
  7. echo '通过';
  8. }
  9. // 双分支
  10. echo '<br>-----------------------双分支-----------------------<br>';
  11. $score = 50;
  12. if ($score > 60) {
  13. echo '通过';
  14. } else {
  15. echo '不通过';
  16. }
  17. // 双分支语法糖
  18. echo '<br>---------------------双分支语法糖------------------<br>';
  19. $score = 90;
  20. echo $score > 60 ? "合格" : "不合格";
  21. // 多分支
  22. echo '<br>-----------------------多分支-----------------------<br>';
  23. if ($score > 60 && $score < 70) {
  24. echo '成绩为:B';
  25. } elseif ($score > 70 && $score < 80) {
  26. echo '成绩为:B+';
  27. } elseif ($score > 80 && $score < 100) {
  28. echo '成绩为:A';
  29. } else {
  30. echo '成绩为:不合格';
  31. }
  32. // 多分支语法糖:switch
  33. echo '<br>------------------多分支语法糖:switch------------<br>';
  34. switch (true) {
  35. case $score < 60:
  36. echo '不满意';
  37. break;
  38. case $score > 60 && $score < 70:
  39. echo '满意';
  40. break;
  41. case $score > 80:
  42. echo '非常满意';
  43. break;
  44. default:
  45. echo '服务太差了';
  46. }
  47. // for循环
  48. echo '<br>-----------------------for循环-----------------------<br>';
  49. $title = ["语文", "数学", "几何", "英语", "地理"];
  50. for ($i = 0; $i < count($title); $i++) {
  51. if ($i === 1) {
  52. continue;
  53. }
  54. echo $title[$i] . '<br>';
  55. }
  56. // foreach循环
  57. echo '<br>-----------------------foreach循环-----------------------<br>';
  58. foreach ($title as $k => $v) {
  59. printf('序号:%s ---> 书名:%s<br>', $k, $v);
  60. }
  61. // while循环
  62. echo '<br>-----------------------while循环-----------------------<br>';
  63. $i = 0;
  64. while ($i < count($title)) {
  65. echo $title[$i] . '<br>';
  66. $i++;
  67. }

运行效果

流程控制演示代码

  1. <?php
  2. namespace _0809;
  3. $cs = [
  4. ['id' => '第一节', 'name' => '覃老师', 'sex' => 0, 'course' => '语文'],
  5. ['id' => '第二节', 'name' => '覃老师', 'sex' => 0, 'course' => '语文'],
  6. ['id' => '第三节', 'name' => '张老师', 'sex' => 1, 'course' => '数学'],
  7. ['id' => '第四节', 'name' => '孙老师', 'sex' => 1, 'course' => '几何'],
  8. ['id' => '第五节', 'name' => '周老师', 'sex' => 1, 'course' => '地理'],
  9. ['id' => '第六节', 'name' => '孔老师', 'sex' => 0, 'course' => '英语'],
  10. ['id' => '第七节', 'name' => '赵老师', 'sex' => 1, 'course' => '体育'],
  11. ['id' => '第八节', 'name' => '马老师', 'sex' => 0, 'course' => '美术'],
  12. ]
  13. ?>
  14. <!DOCTYPE html>
  15. <html lang="zh-CN">
  16. <head>
  17. <meta charset="UTF-8">
  18. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  19. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  20. <title>课程表</title>
  21. <style>
  22. table {
  23. border-collapse: collapse;
  24. width: 360px;
  25. text-align: center;
  26. }
  27. table th,
  28. table td {
  29. border: 1px solid #000;
  30. padding: 5px;
  31. }
  32. table caption {
  33. font-size: 1.3em;
  34. }
  35. table thead {
  36. background-color: lightcyan;
  37. }
  38. .active {
  39. color: red;
  40. }
  41. </style>
  42. </head>
  43. <body>
  44. <table>
  45. <caption>20班课程表</caption>
  46. <thead>
  47. <tr>
  48. <th>节名</th>
  49. <th>教师</th>
  50. <th>性别</th>
  51. <th>课程</th>
  52. </tr>
  53. </thead>
  54. <tbody>
  55. <?php
  56. foreach ($cs as $cso) : ?>
  57. <tr>
  58. <td><?= $cso['id'] ?></td>
  59. <td><?= $cso['name'] ?></td>
  60. <td><?= $cso['sex'] ? '男' : '女' ?></td>
  61. <td><?= $cso['course'] ?></td>
  62. </tr>
  63. <?php endforeach ?>
  64. </tbody>
  65. </table>
  66. </body>
  67. </html>

运行效果

Correcting teacher:PHPzPHPz

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!