ASCII码 ASCII码

php分支与循环 、php与html混编学习

发布于:2022-05-03 00:01:13  栏目:技术文档

一、分支与循环

  1. 分支
  1. $total = 90;
  2. switch(true){
  3. case $total<60:
  4. echo "不合格 <br>";
  5. break;
  6. case $total>60 && $total<80:
  7. echo "合格 <br>";
  8. break;
  9. case $total>90:
  10. echo "优秀 <br>";
  11. break;
  12. default:
  13. echo "中等 <br>";
  14. }

2.循环

  1. $colors = ['red', 'green', 'blue'];
  2. $i = 0;
  3. while ($i < count($colors)) {
  4. $list .= "<li>{$colors[$i]}</li>";
  5. // 更新条件
  6. $i = $i + 1;
  7. }
  8. echo 'while <br>';
  9. echo $list.'<hr>';
  10. $colors2 = ['red', 'green', 'blue'];
  11. for ($i = 0; $i < count($colors); $i++) {
  12. $list2 .= "<li>{$colors2[$i]}</li>";
  13. }
  14. echo 'for <br>';
  15. echo $list2.'<hr>';

二、php模板语法与html混编

  1. php模板语法
  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. <table>
  16. <caption>学生成绩表</caption>
  17. <thead>
  18. <tr>
  19. <th>ID</th>
  20. <th>姓名</th>
  21. <th>课程</th>
  22. <th>成绩</th>
  23. </tr>
  24. </thead>
  25. <tbody>
  26. <!-- 这里显示的用户数据 -->
  27. <?php
  28. foreach ($stus as $stu) {
  29. if ($stu['course'] === 'php') {
  30. echo <<< STU
  31. <tr>
  32. <td>{$stu['id']}</td>
  33. <td>{$stu['name']}</td>
  34. <td class="active">{$stu['course']}</td>
  35. <td>{$stu['score']}</td>
  36. </tr>
  37. STU;
  38. }
  39. }
  40. ?>
  41. </tbody>
  42. </table>
  43. ?>

相关推荐
阅读 +