Blogger Information
Blog 29
fans 0
comment 0
visits 14065
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
分支、循环、混编学习小结
P粉317509817
Original
301 people have browsed it

分支结构

  1. <?php
  2. // 分支结构
  3. // 单分支
  4. $age=18;
  5. if($age>=18){
  6. echo "你好";
  7. }else{
  8. echo "你几岁";
  9. }
  10. echo "<br>";
  11. // 多分支
  12. $age=10;
  13. if($age>=18&&$age<=65){
  14. echo "你好";
  15. }
  16. elseif($age<18){
  17. echo "你好小朋友";
  18. }
  19. elseif($age>65){
  20. echo "你好老人";
  21. }
  22. elseif($age>=110){
  23. echo "你好神仙";
  24. }
  25. else{
  26. echo"请输入正确年龄";
  27. }
  28. echo "<br>";
  29. // switch
  30. $age = 100;
  31. switch(true){
  32. case $age>=18&&$age<=65:
  33. echo "你好";
  34. break;
  35. case $age<18:
  36. echo "你好小朋友";
  37. break;
  38. case $age>65:
  39. echo "你好老人";
  40. break;
  41. case $age>=110:
  42. echo "你好神仙";
  43. break;
  44. default:
  45. echo"请输入正确年龄";
  46. }

效果:

循环和混编

  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="en">
  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>遍历与混编</title>
  23. <style>
  24. table{
  25. border-collapse: collapse;
  26. width: 1000px;
  27. height: 500px;
  28. text-align: center;
  29. margin: auto;
  30. }
  31. table th,
  32. table td {
  33. border: 1px solid #000;
  34. padding: 5px;
  35. }
  36. table caption{
  37. font-size: 1.3em;
  38. }
  39. table thead{
  40. background-color: lightcyan;
  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 foreach($stus as $stu):?>
  57. <tr>
  58. <td><?php echo $stu['id']?></td>
  59. <td><?php echo $stu['name']?></td>
  60. <td><?php echo $stu['course']?></td>
  61. <td><?php echo $stu['score']?></td>
  62. </tr>
  63. <?php endforeach ?>
  64. </tbody>
  65. </table>
  66. </body>
  67. </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