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

1

  1. <!-- 1. 实例演示分支与循环 2. 实例演示php模板语法与html混编技巧 -->
  2. <?php
  3. $age = 28;
  4. $wait = $age-18;
  5. // 1. 单分支
  6. if($age >= 18) echo "welcome" . "<br/>";
  7. // 2. 双分支
  8. if($age > 18) echo "welcome". "<br/>";
  9. else echo "wait" . $wait ."year" . "<br/>";
  10. // 3. 多分支
  11. if($age>=18 && $age<25) echo "welcome" . "<br/>";
  12. elseif($age>=25) echo "Hi Dave" . "<br/>";
  13. else echo "wait" . $wait ."year" . "<br/>";
  14. // 4. 多分支语法糖:switch
  15. switch(true){
  16. case $age>=25:
  17. echo "Hello Dave" . "<br/>";
  18. break;
  19. default:
  20. echo "wait" . $wait ."year" . "<br/>";
  21. }
  22. // 循环
  23. // while
  24. $i = 0;
  25. while($i<5){
  26. echo "有".$i."只小猪"."<br/>";
  27. $i++;
  28. }
  29. echo "<hr>";
  30. // do-while
  31. $i = 0;
  32. do{
  33. echo "有".$i."只小猪"."<br/>";
  34. $i++;
  35. }while($i<5);
  36. echo "<hr>";
  37. // for
  38. for($i=0;$i<5;$i++){
  39. echo "有".$i."只小猪"."<br/>";
  40. }

2

  1. <?php
  2. $user_freshman=[
  3. ["id"=>"0","name"=>"Frank","course"=>"java","score"=>"89"],
  4. ["id"=>"1","name"=>"John","course"=>"java","score"=>"89"],
  5. ["id"=>"2","name"=>"Jane","course"=>"java","score"=>"89"],
  6. ["id"=>"3","name"=>"Dave","course"=>"php","score"=>"99"],
  7. ["id"=>"4","name"=>"David","course"=>"C","score"=>"79"],
  8. ];
  9. $user_sophomore=[
  10. ["id"=>"0","name"=>"Frank","course"=>"python","score"=>"89"],
  11. ["id"=>"1","name"=>"John","course"=>"big data","score"=>"89"],
  12. ["id"=>"2","name"=>"Jane","course"=>"graph","score"=>"89"],
  13. ["id"=>"3","name"=>"Dave","course"=>"web","score"=>"99"],
  14. ["id"=>"4","name"=>"David","course"=>"C++","score"=>"79"],
  15. ];
  16. ?>
  17. <!DOCTYPE html>
  18. <html lang="en">
  19. <head>
  20. <meta charset="UTF-8">
  21. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  22. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  23. <title>php模板语法与html混编</title>
  24. <style>
  25. table {
  26. border-collapse: collapse;
  27. width: 360px;
  28. text-align: center;
  29. }
  30. table th,
  31. table td {
  32. border: 1px solid #000;
  33. padding: 5px;
  34. }
  35. table caption {
  36. font-size: 1.3em;
  37. }
  38. table thead {
  39. background-color: lightcyan;
  40. }
  41. .active {
  42. color: red;
  43. }
  44. </style>
  45. </head>
  46. <body>
  47. <table>
  48. <caption>大一成绩表</caption>
  49. <thead>
  50. <tr>
  51. <th>学号</th>
  52. <th>姓名</th>
  53. <th>课程</th>
  54. <th>成绩</th>
  55. </tr>
  56. </thead>
  57. <tbody>
  58. <!-- 大一 -->
  59. <!-- foreach -->
  60. <?php
  61. $list=null;
  62. foreach($user_freshman as $users){
  63. $table = "<tr>";
  64. $table.="<th>{$users["id"]}</th>";
  65. $table.="<th>{$users["name"]}</th>";
  66. $table.="<th>{$users["course"]}</th>";
  67. $table.="<th>{$users["score"]}</th>";
  68. $table.= "</tr>";
  69. $list.=$table;
  70. }
  71. echo $list
  72. ?>
  73. </tbody>
  74. </table>
  75. <table>
  76. <caption>大二成绩表</caption>
  77. <thead>
  78. <tr>
  79. <th>学号</th>
  80. <th>姓名</th>
  81. <th>课程</th>
  82. <th>成绩</th>
  83. </tr>
  84. </thead>
  85. <tbody>
  86. <!-- 大二 -->
  87. <?php foreach($user_sophomore as $users):?>
  88. <tr>
  89. <th><?= $users["id"]?></th>
  90. <th><?= $users["name"]?></th>
  91. <th><?= $users["course"]?></th>
  92. <th><?= $users["score"]?></th>
  93. </tr>
  94. <?php endforeach ?>
  95. </tbody>
  96. </table>
  97. </body>
  98. </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!