博客列表 >流程控制与模板语法

流程控制与模板语法

吴长清
吴长清原创
2022年08月10日 10:52:31372浏览

1.流程控制

1.1 分支判断

  1. <?php
  2. namespace _0809;
  3. $a = 10;
  4. $b = 20;
  5. // 1.单分支
  6. echo "1.单分支: ";
  7. if ($a < $b) {
  8. echo " $b 大于 $a <hr>";
  9. }
  10. // 2.双分支
  11. echo "2.双分支: ";
  12. $b = 5;
  13. if ($a < $b) {
  14. echo " $b 大于 $a <hr>";
  15. } else {
  16. echo " $b 小于 $a <hr>";
  17. }
  18. // 3.双分支语法糖:三元运算符
  19. echo "3.双分支语法糖: 三元运算符 : ";
  20. echo $a < $b ? " $b 大于 $a <hr>" : "$b 小于 $a <hr>";
  21. // 4.多分支
  22. echo "4.多分支: ";
  23. $b = 10;
  24. if ($a < $b) {
  25. echo " $b 大于 $a <hr>";
  26. } elseif ($a > $b) {
  27. echo " $b 小于 $a <hr>";
  28. } else {
  29. echo " $b 等于 $a <hr>";
  30. }
  31. // 5.多分支语法糖:switch
  32. echo "5.多分支语法糖: switch : ";
  33. switch (true) {
  34. case $a < $b:
  35. echo " $b 大于 $a <hr>";
  36. break;
  37. case $a > $b:
  38. echo " $b 小于 $a <hr>";
  39. break;
  40. default:
  41. echo " $b 等于 $a <hr>";
  42. }

1.2 循环

  1. <?php
  2. namespace _0809;
  3. $arr = ["张三", "男", "zhangsan@qq.com", "贵州安顺", 18];
  4. // 1.for循环
  5. echo '1.for: <br>';
  6. for ($i = 0; $i < count($arr); $i++) {
  7. //不输出索引为1的值
  8. if ($i === 1) {
  9. continue;
  10. }
  11. echo $arr[$i] . '<br>';
  12. }
  13. // 2.foreach
  14. echo '<hr>2.foreach: <br>';
  15. foreach ($arr as $k => $v) {
  16. printf('key: %s => value: %s <br>', $k, $v);
  17. }
  18. // 3.while
  19. echo '<hr>2.while: <br>';
  20. $i = 0;
  21. while ($i < count($arr)) {
  22. echo $arr[$i] . '<br>';
  23. $i++;
  24. }

2.模板语法

foreach渲染

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>模板渲染</title>
  8. <style>
  9. table {
  10. border-collapse: collapse;
  11. /* width: 360px; */
  12. text-align: center;
  13. margin: auto;
  14. }
  15. table th,
  16. table td {
  17. border: 1px solid #000;
  18. padding: 5px;
  19. }
  20. table caption {
  21. font-size: 1.3em;
  22. }
  23. table thead {
  24. background-color: lightcyan;
  25. }
  26. .active {
  27. color: red;
  28. }
  29. </style>
  30. </head>
  31. <body>
  32. <div class="app">
  33. <table>
  34. <caption>学生信息表</caption>
  35. <thead>
  36. <tr>
  37. <th><input type="checkbox" name="" id=""></th>
  38. <th>ID</th>
  39. <th>姓名</th>
  40. <th>性别</th>
  41. <th>年龄</th>
  42. <th>入学时间</th>
  43. <th>课程</th>
  44. <th>班级</th>
  45. <th>地址</th>
  46. <th>邮箱</th>
  47. </tr>
  48. </thead>
  49. <tbody>
  50. <?php
  51. $stuInfo = [
  52. ['id' => 1, 'name' => "赵大", 'sex' => 0, 'age' => 18, 'studyDate' => '2022.9', 'major' => 'PHP', 'class' => '2201', 'address' => '贵州贵阳', 'email' => 'zhaoda@qq.com'],
  53. ['id' => 2, 'name' => "王二", 'sex' => 0, 'age' => 18, 'studyDate' => '2022.9', 'major' => 'PHP', 'class' => '2201', 'address' => '贵州遵义', 'email' => 'wanger@qq.com'],
  54. ['id' => 3, 'name' => "张三", 'sex' => 1, 'age' => 18, 'studyDate' => '2022.9', 'major' => 'PHP', 'class' => '2201', 'address' => '贵州六盘水', 'email' => 'zhangsan@qq.com'],
  55. ['id' => 4, 'name' => "李四", 'sex' => 0, 'age' => 19, 'studyDate' => '2021.9', 'major' => 'Java', 'class' => '2101', 'address' => '贵州安顺', 'email' => 'lisi@qq.com'],
  56. ['id' => 5, 'name' => "钱五", 'sex' => 1, 'age' => 19, 'studyDate' => '2021.9', 'major' => 'Python', 'class' => '2102', 'address' => '贵州毕节', 'email' => 'qianwu@qq.com'],
  57. ['id' => 6, 'name' => "吕六", 'sex' => 1, 'age' => 20, 'studyDate' => '2021.9', 'major' => 'Python', 'class' => '2102', 'address' => '贵州铜仁', 'email' => 'lvliu@qq.com'],
  58. ];
  59. ?>
  60. <?php foreach ($stuInfo as $stu) : ?>
  61. <tr>
  62. <td><input type="checkbox" name="" id=""></td>
  63. <td><?= $stu['id'] ?></td>
  64. <td><?= $stu['name'] ?></td>
  65. <!-- 0:女 1:男 -->
  66. <td><?= $stu['sex'] ? '男' : '女' ?></td>
  67. <td><?= $stu['age'] ?></td>
  68. <td><?= $stu['studyDate'] ?></td>
  69. <td><?= $stu['major'] ?></td>
  70. <td><?= $stu['class'] ?></td>
  71. <td><?= $stu['address'] ?></td>
  72. <td><?= $stu['email'] ?></td>
  73. </tr>
  74. <?php endforeach ?>
  75. </tbody>
  76. </table>
  77. </div>
  78. </body>
  79. </html>

switch渲染

  1. <body>
  2. <form action="" method="POST">
  3. <label>请输入您的年龄:</label>
  4. <input type="number" name="text" id="">
  5. <input type="submit" value="确定" .name='sub'>
  6. <br>
  7. <br>
  8. </form>
  9. <?php
  10. if (empty($_POST['sub'])) {
  11. $age = $_POST['text'];
  12. switch (true) {
  13. case $age > 7 && $age <= 12:
  14. echo " <span>你现在是 $age 岁,应该在读小学!</span>";
  15. break;
  16. case $age > 12 && $age <= 18:
  17. echo " <span>你现在是 $age 岁,应该在读中学!</span>";
  18. break;
  19. case $age > 18 && $age <= 28:
  20. echo " <span>你现在是 $age 岁,应该在读大学或者研究生!</span>";
  21. break;
  22. case $age > 28 && $age <= 38:
  23. echo " <span>你现在是 $age 岁,应该结婚了,同时应该在还房贷了!</span>";
  24. break;
  25. case $age > 38 && $age <= 58:
  26. echo " <span>你现在是 $age 岁,房贷还完了,应该要操心你儿子的彩礼钱了和房贷了!</span>";
  27. break;
  28. case $age > 58 && $age <= 98:
  29. echo " <span>你现在是 $age 岁,儿子孝顺,天天打麻将,不孝顺天天骂儿子</span>";
  30. break;
  31. case $age > 100 || $age <= 0:
  32. echo " <span>$age 岁 是一个非法年龄</span>";
  33. break;
  34. default:
  35. echo " <span>你现在是 $age 岁,还是个宝宝";
  36. }
  37. }
  38. ?>
  39. </body>

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议