Blogger Information
Blog 20
fans 0
comment 0
visits 7923
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php分支与循环/模板用法
P粉191340380
Original
340 people have browsed it

分支与循环

  1. // 单分支
  2. $age = 19;
  3. if($age >= 18){
  4. echo '满18岁,已成年'. '<br>';
  5. }
  6. // 双分支
  7. $age = 12;
  8. if($age >= 18){
  9. echo '满18岁,你已成年<br>';
  10. } else{
  11. echo '你未成年,请家长陪同<br>';
  12. }
  13. // 双分支语法糖: 三元运算符
  14. echo $age >=18 ? "满18岁,你已成年" : "你未成年,请家长陪同";
  15. echo '<br>';
  16. // 多分支
  17. $age = 49;
  18. if ($age < 18){
  19. echo '你还是努力上学吧<br>';
  20. } elseif ($age = 18){
  21. echo '买房的钱存够了吗<br>';
  22. } elseif ($age > 50){
  23. echo '房贷供完了吗';
  24. }
  25. // 多分支语法糖 switch
  26. $age = 61;
  27. switch(true){
  28. case $age > 12 && $age <=24;
  29. echo '还在上学奋斗中';
  30. break;
  31. case $age > 25 && $age <=40;
  32. echo '还在苦逼打工';
  33. break;
  34. case $age > 41 && $age <=65;
  35. echo '在公司养老中';
  36. break;
  37. case $age > 66 && $age <=80;
  38. echo '退休中';
  39. break;
  40. }

模板语法

  1. namespace _0809;
  2. // 用二维数组来模拟数据表查询结果
  3. $stus = [
  4. ['id' => 1, 'name' => '张三', 'gender' => '男', 'score' => 83],
  5. ['id' => 2, 'name' => '李四', 'gender' => '女', 'score' => 66],
  6. ['id' => 3, 'name' => '刘五', 'gender' => '男', 'score' => 42],
  7. ['id' => 4, 'name' => '钱六', 'gender' => '女', 'score' => 90],
  8. ['id' => 5, 'name' => '陈七', 'gender' => '男', 'score' => 55],
  9. ['id' => 6, 'name' => '赵八', 'gender' => '女', 'score' => 45],
  10. ['id' => 7, 'name' => '马九', 'gender' => '男', 'score' => 75],
  11. ['id' => 8, 'name' => '陆十', 'gender' => '男', 'score' => 88],
  12. ]
  13. ?>
  14. <!DOCTYPE html>
  15. <html lang="en">
  16. <head>
  17. <meta charset="UTF-8">
  18. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  19. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  20. <title>学生信息表</title>
  21. <style>
  22. table {
  23. border-collapse: collapse;
  24. width: 360px;
  25. text-align: center;
  26. }
  27. table th,
  28. table td {
  29. border: 1px solid #000;
  30. padding: 5px;
  31. }
  32. table caption {
  33. font-size: 1.3em;
  34. }
  35. table thead {
  36. background-color: lightcyan;
  37. }
  38. .active {
  39. color: red;
  40. }
  41. </style>
  42. </head>
  43. <body>
  44. <table>
  45. <thead>
  46. <tr>
  47. <th>ID</th>
  48. <th>姓名</th>
  49. <th>性别</th>
  50. <th>成绩</th>
  51. </tr>
  52. </thead>
  53. <tbody>
  54. <?php foreach($stus as $stu): ?>
  55. <tr>
  56. <td><?=$stu['id']?></td>
  57. <td><?=$stu['name']?></td>
  58. <td><?=$stu['gender']?></td>
  59. <td><?=$stu['score']?></td>
  60. </tr>
  61. <?php endforeach ?>
  62. </tbody>
  63. </table>
  64. </body>
  65. </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!