Blogger Information
Blog 33
fans 0
comment 0
visits 17096
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
分支循环,php 模板语法与 html 混编技巧
lucaslwk
Original
433 people have browsed it

分支循环,php 模板语法与 html 混编技巧

一.分支循环

分支循环

  1. <?php
  2. //单分支
  3. $age = 20;
  4. if ($age >= 18) {
  5. echo "用户已成年<br>";
  6. }
  7. //双分支
  8. $age = 15;
  9. if ($age >= 18) {
  10. echo "用户已成年<br>";
  11. } else {
  12. echo "用户未成年<br>";
  13. }
  14. //三元表达式
  15. $age = 25;
  16. echo ($age >= 15) ? "用户已成年<br>" : "用户未成年<br>";
  17. //多分支
  18. $age = 35;
  19. if ($age <= 18) {
  20. echo "用户未成年<br>";
  21. } else if ($age >= 18 && $age <= 30) {
  22. echo "用户已成年,每天适当游戏时间<br>";
  23. } else {
  24. echo "用户已成年,合理安排游戏时间<br>";
  25. };
  26. //多分支的语法糖
  27. $age = 28;
  28. switch (true) {
  29. case $age <= 18:
  30. "用户未成年<br>";
  31. break;
  32. case $age >= 18 && $age <= 30:
  33. echo "用户已成年,每天适当游戏时间<br>";
  34. break;
  35. default:
  36. echo "用户已成年,合理安排游戏时间<br>";
  37. break;
  38. }
  39. echo "<hr>";
  40. //循环
  41. $colors = ['red', 'green', 'blue'];
  42. //初始化循环变量
  43. $i = 0;
  44. //设置循环条件
  45. while ($i < count($colors)) {
  46. echo $colors[$i] . "<br>";
  47. //更新循环条件
  48. $i = $i + 1;
  49. }
  50. //循环语法糖for
  51. for ($i = 0; $i < count($colors); $i++) {
  52. echo $colors[$i] . "<br>";
  53. }
  54. echo "<hr>";
  55. //中断或跳过某次循环
  56. $colors = ['red', 'green', 'blue', 'red', 'green', 'blue'];
  57. for ($i = 0; $i < count($colors); $i++) {
  58. //只输出前五个
  59. if ($i > 4) break;
  60. //跳过第二个
  61. if ($i === 1) continue;
  62. echo $colors[$i] . "<br>";
  63. }

二.php 模板语法与 html 混编技巧

php模板语法与html混编技巧

  1. <?php
  2. $stus = [
  3. ['id' => 1, 'name' => '刘备', 'course' => 'js', 'score' => 83],
  4. ['id' => 2, 'name' => '关羽', 'course' => 'php', 'score' => 75],
  5. ['id' => 3, 'name' => '张飞', 'course' => 'js', 'score' => 52],
  6. ['id' => 4, 'name' => '孙权', 'course' => 'php', 'score' => 88],
  7. ['id' => 5, 'name' => '周瑜', 'course' => 'js', 'score' => 65],
  8. ['id' => 6, 'name' => '孔明', 'course' => 'php', 'score' => 53],
  9. ['id' => 7, 'name' => '赵云', 'course' => 'js', 'score' => 63],
  10. ['id' => 8, 'name' => '马超', 'course' => 'js', 'score' => 77],
  11. ['id' => 9, 'name' => '姜维', 'course' => 'php', 'score' => 93],
  12. ['id' => 10, 'name' => '黄忠', 'course' => 'js', 'score' => 81],
  13. ]
  14. ?>
  15. <!DOCTYPE html>
  16. <html lang="en">
  17. <head>
  18. <meta charset="UTF-8">
  19. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  20. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  21. <title>php模板语法与html混编技巧</title>
  22. <style>
  23. table {
  24. border-collapse: collapse;
  25. border: 1px solid black;
  26. text-align: center;
  27. width: 400px;
  28. margin: 0 auto;
  29. }
  30. table th,
  31. table td {
  32. border: 1px solid black;
  33. }
  34. .js_course {
  35. background-color: darkcyan;
  36. }
  37. .php_course {
  38. background-color: burlywood;
  39. }
  40. .un_score {
  41. background-color: red;
  42. color: white;
  43. }
  44. </style>
  45. </head>
  46. <body>
  47. <table>
  48. <caption>学习成绩表</caption>
  49. <thead>
  50. <th>ID</th>
  51. <th>姓名</th>
  52. <th>课程</th>
  53. <th>分数</th>
  54. </thead>
  55. <tbody>
  56. <!-- 数组的遍历,foreach(数组 as 键名 =>值){...} -->
  57. <?php foreach ($stus as $stu) : ?>
  58. <tr>
  59. <!-- <?php echo "123" ?> 简化为 <?= "123" ?> -->
  60. <td><?= $stu["id"] ?></td>
  61. <td><?= $stu["name"] ?></td>
  62. <?php $course_type = ($stu["course"] === "js") ? "js_course" : "php_course" ?>
  63. <td class=<?= $course_type ?>><?= $stu["course"] ?></td>
  64. <?php $score_type = ($stu["score"] < 60) ? "un_score" : "" ?>
  65. <td class=<?= $score_type ?>><?= $stu["score"] ?></td>
  66. </tr>
  67. <?php endforeach ?>
  68. </tbody>
  69. </table>
  70. </body>
  71. </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