Blogger Information
Blog 15
fans 0
comment 0
visits 6253
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
流程控制:分支和循环,php与html混编技巧
啊℃。㏄
Original
439 people have browsed it

分支

  1. <?php
  2. // ! 流程控制: 分支
  3. $age = 15;
  4. // * 1. 单分支
  5. if ($age >= 18) {
  6. echo '恭喜, 已成年,可以观看<br>';
  7. }
  8. // * 2. 双分支
  9. // $age = 38;
  10. if ($age >= 18)
  11. echo '恭喜, 已成年,可以观看<br>';
  12. else
  13. // 默认分支
  14. echo '未成年, 请在家长陪同下观看<br>';
  15. // * 3. 多分支
  16. $age = 44;
  17. if ($age >= 18 && $age < 30)
  18. echo "{$age}岁, 彩礼, 能按揭吗? <br>";
  19. else if ($age >= 30 && $age < 45)
  20. echo "{$age}岁, 应该成个家了 <br>";
  21. else if ($age >= 45)
  22. echo "{$age}岁, 房贷快还完了 <br>";
  23. // 未成年, < 18, 默认分支
  24. else
  25. echo "{$age}岁, 放学了, 我送你回家 <br>";
  26. // * 4. 多分支的语法糖:switch
  27. $age = 15;
  28. switch (true) {
  29. case $age >= 18 && $age < 30:
  30. echo "{$age}岁, 彩礼, 能按揭吗? <br>";
  31. break;
  32. case $age >= 30 && $age < 45:
  33. echo "{$age}岁, 应该成个家了 <br>";
  34. break;
  35. case $age >= 45:
  36. echo "{$age}岁, 房贷快还完了 <br>";
  37. break;
  38. default:
  39. echo "{$age}岁, 放学了, 我送你回家 <br>";
  40. }

循环

  1. <?php
  2. // ! 流程控制: 循环
  3. // 循环本质上还是"分支"
  4. $colors = ['red', 'green', 'blue'];
  5. /**
  6. * 循环三要素:
  7. * 1. 初始化循环变量: $i =0;
  8. * 2. 循环条件: $i < count($colors);
  9. * 3. 更新循环条件: $i = $i + 1
  10. */
  11. // * while() 来简化以上的分支过程
  12. $list = '<ul style="border:1px solid;background: lightcyan">';
  13. $i = 0;
  14. while ($i < count($colors)) {
  15. $list .= "<li>{$colors[$i]}</li>";
  16. // 更新条件
  17. $i = $i + 1;
  18. }
  19. $list .= '</ul>';
  20. echo $list;
  21. // while 还有一个双胞胎, do-while 与上面的区别在于条件判断的时机不同
  22. $list = '<ul style="border:1px solid;background: lightgreen">';
  23. $i = 0;
  24. do {
  25. $list .= "<li>{$colors[$i]}</li>";
  26. // 更新条件
  27. $i = $i + 1;
  28. } while ($i > count($colors));
  29. $list .= '</ul>';
  30. echo $list;
  31. // for 可看成 while 的语法糖
  32. $list = '<ul style="border:1px solid;background: violet">';
  33. for ($i = 0; $i < count($colors); $i++) {
  34. $list .= "<li>{$colors[$i]}</li>";
  35. }
  36. $list .= '</ul>';
  37. echo $list;
  38. // 中断或跳过某次循环
  39. // break; continue
  40. $list = '<ul style="border:1px solid;background: pink">';
  41. for ($i = 0; $i < count($colors); $i++) {
  42. // 只输出前二个
  43. // if ($i > 1) break;
  44. // 跳过第2个,只输出第1个和第3个
  45. if ($i === 1) continue;
  46. $list .= "<li>{$colors[$i]}</li>";
  47. }
  48. $list .= '</ul>';
  49. echo $list;

php与html原生混编技巧

  1. <?php
  2. // 用二维数组来模拟数据表查询结果集
  3. $stus = [
  4. ['id' => 1, 'name' => '刘备', 'course' => 'js', 'score' => 83],
  5. ['id' => 2, 'name' => '关羽', 'course' => 'php', 'score' => 75],
  6. ['id' => 3, 'name' => '张飞', 'course' => 'js', 'score' => 52],
  7. ['id' => 4, 'name' => '孙权', 'course' => 'php', 'score' => 88],
  8. ['id' => 5, 'name' => '周瑜', 'course' => 'js', 'score' => 65],
  9. ['id' => 6, 'name' => '孔明', 'course' => 'php', 'score' => 53],
  10. ['id' => 7, 'name' => '赵云', 'course' => 'js', 'score' => 63],
  11. ['id' => 8, 'name' => '马超', 'course' => 'js', 'score' => 77],
  12. ['id' => 9, 'name' => '姜维', 'course' => 'php', 'score' => 93],
  13. ['id' => 10, 'name' => '黄忠', 'course' => 'js', 'score' => 81],
  14. ]
  15. ?>
  16. <!DOCTYPE html>
  17. <html lang="zh-CN">
  18. <head>
  19. <meta charset="UTF-8">
  20. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  21. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  22. <title>php与html原生混编</title>
  23. <style>
  24. table {
  25. border-collapse: collapse;
  26. width: 360px;
  27. text-align: center;
  28. }
  29. table th,
  30. table td {
  31. border: 1px solid #000;
  32. padding: 5px;
  33. }
  34. table caption {
  35. font-size: 1.3em;
  36. }
  37. table thead {
  38. background-color: lightcyan;
  39. }
  40. .active {
  41. color: red;
  42. }
  43. </style>
  44. </head>
  45. <body>
  46. <table>
  47. <caption>学生成绩表</caption>
  48. <thead>
  49. <tr>
  50. <th>ID</th>
  51. <th>姓名</th>
  52. <th>课程</th>
  53. <th>成绩</th>
  54. </tr>
  55. </thead>
  56. <tbody>
  57. <!-- 这里显示的用户数据 -->
  58. <?php
  59. foreach ($stus as $stu) {
  60. // 只查php
  61. if ($stu['course'] === 'php') {
  62. echo <<< STU
  63. <tr>
  64. <td>{$stu['id']}</td>
  65. <td>{$stu['name']}</td>
  66. <td class="active">{$stu['course']}</td>
  67. <td>{$stu['score']}</td>
  68. </tr>
  69. STU;
  70. }
  71. }
  72. ?>
  73. </tbody>
  74. </table>
  75. </body>
  76. </html>

模板混编

  1. <!-- php模板语法的目标:与html和php代码分离 -->
  2. <?php foreach($stus as $stu) : ?>
  3. <!-- 当前已经离开了php环境,处在html中了 -->
  4. <!-- <tr>
  5. <td><?php echo $stu['id'] ?></td>
  6. <td><?php echo $stu['name'] ?></td>
  7. <td><? echo $stu['course'] ?></td>
  8. <td><? echo $stu['score'] ?></td>
  9. </tr> -->
  10. <!-- 输出成绩大于70分的 -->
  11. <!-- <?php if($stu['score'] > 70) :?>
  12. <tr>
  13. <td><?php echo $stu['id'] ?></td>
  14. <td><?php echo $stu['name'] ?></td>
  15. <td><? echo $stu['course'] ?></td>
  16. <td class="active"><? echo $stu['score'] ?></td>
  17. </tr>
  18. <?php endif ?> -->
  19. <!-- 输出全部,并将不合格的成绩标红 -->
  20. <tr>
  21. <td><?php echo $stu['id'] ?></td>
  22. <td><?php echo $stu['name'] ?></td>
  23. <td><? echo $stu['course'] ?></td>
  24. <td class="<?= $stu['score'] < 60 ? "active" : '' ?>"><? echo $stu['score'] ?></td>
  25. </tr>
  26. <!-- 动态设置样式方法 -->

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