ASCII码 ASCII码

分支与循环,PHP与HTML混编

发布于:2022-04-21 14:56:12  栏目:技术文档

单分支

  1. $age = 18;
  2. if($age >=18){
  3. echo '恭喜,你已成年,游戏时长不受限制';
  4. }
  5. //输出结果:恭喜。你已成年,游戏时长不受限制

双分支

  1. $age = 15;
  2. if($age >= 18){
  3. echo '恭喜,你已成年,游戏时长不受限制';
  4. }else{
  5. echo '你未成年,游戏时长为2小时';
  6. }
  7. //输出结果:你未成年,游戏时长为2小时

多分支

  1. $age = 20;
  2. if($age >= 18 && $age <30){
  3. echo $age.'岁,游戏时长没限制';
  4. }else if( $age >= 30 && $age < 45){
  5. echo $age.'岁,少玩点游戏';
  6. }else if($age>=45){
  7. echo $age.'岁,别玩游戏了,出去走走吧';
  8. }else{
  9. echo $age.'岁,别玩游戏,多陪陪家人';
  10. }
  11. //输出结果:20岁,游戏不受限制

多分支语法糖:switch

  1. $age = 15;
  2. switch(true){
  3. case $age >= 18 && $age <30:
  4. echo $age.'岁,游戏时长没限制';
  5. break;
  6. case $age >= 30 && $age < 45:
  7. echo $age.'岁,少玩点游戏';
  8. break;
  9. case $age>=45:
  10. echo $age.'岁,别玩游戏了,出去走走吧';
  11. break;
  12. default:
  13. echo $age.'岁,别玩游戏,多陪陪家人';
  14. }
  15. //输出结果:15岁,别玩游戏,陪陪家人

循环:while,将数组输出成列表

  1. $colors = ['red','green','blue'];
  2. $list = '<ul style="border:1px solid;background:lightcyan">';
  3. $i = 0;
  4. while($i<count($colors)){
  5. $list .= '<li>{$colors[$i]}</li>';
  6. $i =$i + 1;
  7. }
  8. $list .= </ul>;
  9. echo $list;

循环:do while

  1. $colors = ['red','green','blue'];
  2. $list = '<ul style="border:1px solid;background:lightcyan">';
  3. $i = 0;
  4. do{
  5. $list .= '<li>{$colors[$i]}</li>';
  6. $i =$i + 1;
  7. }while($i<count($colors));
  8. $list .= </ul>;
  9. echo $list;

循环:for

  1. $colors = ['red','green','blue'];
  2. $list = '<ul style="border:1px solid;background:violet">';
  3. for($i = 0 ; $i< count($colors);$i++){
  4. $list .= "<li>{$colors[$i]}</li>";
  5. }
  6. $list .= </ul>;
  7. echo $list;

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. width: 360px;
  26. text-align: center;
  27. }
  28. table th,
  29. table td {
  30. border: 1px solid #000;
  31. padding: 5px;
  32. }
  33. table caption {
  34. font-size: 1.3em;
  35. }
  36. table thead {
  37. background-color: lightcyan;
  38. }
  39. .active {
  40. color: red;
  41. }
  42. </style>
  43. </head>
  44. <body>
  45. <table>
  46. <caption>学生成绩表</caption>
  47. <thead>
  48. <tr>
  49. <th>ID</th>
  50. <th>姓名</th>
  51. <th>课程</th>
  52. <th>成绩</th>
  53. </tr>
  54. </thead>
  55. <tbody>
  56. <?php
  57. foreach($stus as $stu){
  58. // echo "<tr>";
  59. // echo "<td>{$stu['id']}</td>";
  60. // echo "<td>{$stu['name']}</td>";
  61. // echo "<td>{$stu['course']}</td>";
  62. // echo "<td>{$stu['score']}</td>";
  63. // heredoc, 写模板, 可以解析内部变量
  64. echo <<< STU
  65. <tr>
  66. <td>{$stu['id']}</td>
  67. <td>{$stu['name']}</td>
  68. <td>{$stu['course']}</td>
  69. <td>{$stu['score']}</td>
  70. </tr>
  71. STU;
  72. //!只查某种元素
  73. // if($stu['course']==='php'){
  74. // echo <<< STU
  75. // <tr>
  76. // <td>{$stu['id']}</td>
  77. // <td>{$stu['name']}</td>
  78. // <td>{$stu['course']}</td>
  79. // <td>{$stu['score']}</td>
  80. // </tr>
  81. // STU;
  82. // }
  83. }
  84. ?>
  85. </tbody>
  86. </table>
  87. </body>
  88. </html>
相关推荐
阅读 +