Blogger Information
Blog 30
fans 0
comment 0
visits 13825
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP分支、循环、php模板语法、html混编技巧
天宁
Original
598 people have browsed it

流程控制:分支

单分支

  1. $age = 15;
  2. if ($age >= 18) {
  3. echo '恭喜, 已成年,可以观看<br>';
  4. }

双分支

  1. if ($age >= 18) {
  2. echo '恭喜, 已成年,可以观看<br>';
  3. } else {
  4. //默认分支
  5. echo '未成年, 请在家长陪同下观看<br>';
  6. }
  7. //如果分支只有一行代码,可以不写大括号
  8. if ($age >= 18)
  9. echo '恭喜, 已成年,可以观看<br>';
  10. else
  11. // 默认分支
  12. echo '未成年, 请在家长陪同下观看<br>';

多分支

  1. $age = 44;
  2. if ($age >= 18 && $age < 30)
  3. echo "{$age}岁, 彩礼, 能按揭吗? <br>";
  4. else if ($age >= 30 && $age < 45)
  5. echo "{$age}岁, 应该成个家了 <br>";
  6. else if ($age >= 45)
  7. echo "{$age}岁, 房贷快还完了 <br>";
  8. else
  9. // 未成年, < 18, 默认分支
  10. echo "{$age}岁, 放学了, 我送你回家 <br>";

多分支语法糖:switch

  1. $age = 15;
  2. switch (true) {
  3. case $age >= 18 && $age < 30:
  4. echo "{$age}岁, 彩礼, 能按揭吗? <br>";
  5. break;
  6. case $age >= 30 && $age < 45:
  7. echo "{$age}岁, 应该成个家了 <br>";
  8. break;
  9. case $age >= 45:
  10. echo "{$age}岁, 房贷快还完了 <br>";
  11. break;
  12. default:
  13. echo "{$age}岁, 放学了, 我送你回家 <br>";
  14. }

流程控制:循环

循环本质上还是”分支”

  1. 初始化循环变量,这里用索引当循环变量,$i=0;指向第一个数组元素

    $i = 0;

  2. 循环条件:数组长度 = 最大索引 + 1

    1. echo count($colors);
    2. if ($i < count($colors)) {
    3. echo $colors[$i] . '<br>';
    4. }
  3. 更新循环条件

    1. $i = $i + 1;
    2. if ($i < count($colors)) {
    3. echo $colors[$i] . '<br>';
    4. }
    5. $i = $i + 1;
    6. if ($i < count($colors)) {
    7. echo $colors[$i] . '<br>';
    8. }
    9. echo '<hr>';
  4. 总结循环三要素

    1. 初始化循环变量: $i =0;
    2. 循环条件: $i < count($colors);
    3. 更新循环条件:$i = $i + 1

while() 来简化以上的分支过程

  1. while(),先判断再循环

    1. $list = '<ul style="border:1px solid;background: lightcyan">';
    2. $i = 0;
    3. while ($i < count($colors)) {
    4. $list .= "<li>{$colors[$i]}</li>";
    5. // 更新条件
    6. $i = $i + 1;
    7. }
    8. $list .= '</ul>';
    9. echo $list;
  2. do-while 与上面的区别在于条件判断的时机不同,先循环再判断

    1. $list = '<ul style="border:1px solid;background: lightgreen">';
    2. $i = 0;
    3. do {
    4. $list .= "<li>{$colors[$i]}</li>";
    5. // 更新条件
    6. $i = $i + 1;
    7. } while ($i > count($colors));
    8. $list .= '</ul>';
    9. echo $list;
  3. for 可看成 while 的语法糖

    1. $list = '<ul style="border:1px solid;background: violet">';
    2. for ($i = 0; $i < count($colors); $i++) {
    3. $list .= "<li>{$colors[$i]}</li>";
    4. }
    5. $list .= '</ul>';
    6. echo $list;
    7. // 中断(break)或跳过某次循环(continue)
    8. $list = '<ul style="border:1px solid;background: pink">';
    9. for ($i = 0; $i < count($colors); $i++) {
    10. // 只输出前二个
    11. // if ($i > 1) break;
    12. // 跳过第2个,只输出第1个和第3个
    13. if ($i === 1) continue;
    14. $list .= "<li>{$colors[$i]}</li>";
    15. }
    16. $list .= '</ul>';
    17. echo $list;

数组声明与遍历

数组的声明

  1. $colors = ['red', 'green', 'blue'];
索引数组:键名是从0开始的递增的整数
  1. $colors = [0 => 'red', 1 => 'green', 2 => 'blue'];
  2. printf('<pre>%s</pre>', print_r($colors, true));
关联数组:键名的字符串
  1. $user = ['id' => 5, 'name' => '猪老师', 'score' => 90];
  2. printf('<pre>%s</pre>', print_r($user, true));
  3. echo $user['name'];
二者关系

索引数组实际上关联数组的一个子集,只不过用了数字型的字符串

  1. $colors = ['0' => 'red', '1' => 'green', '2' => 'blue'];
  2. printf('<pre>%s</pre>', print_r($colors, true));
  3. //下面两个都能获取到值green
  4. echo $colors[1], '<br>';
  5. echo $colors['1'], '<br>';

数组遍历

while,for
数组成员还可以是一个数组,多维数组: 二维数组
数据表的查询结果,就是用”二维数组”来表示

foreach (数组 as 键名=>值) {…} , 键名是可选的
  1. $users = [
  2. 0 => ['id' => 5, 'name' => '猪老师', 'gender' => 0, 'age' => 18],
  3. 1 => ['id' => 6, 'name' => '牛老师', 'gender' => 1, 'age' => 30],
  4. 2 => ['id' => 5, 'name' => '狗老师', 'gender' => 0, 'age' => 35],
  5. ];
  6. $table = '<table border="1" width="400" cellspacing="0" cellpadding="3" align="center">';
  7. $table .= '<caption>用户信息表</caption>';
  8. $table .= '<thead bgcolor="#ccc"><tr><th>id</th><th>用户名</th><th>性别</th><th>年龄</th></tr></thead>';
  9. $table .= '<tbody align="center">';
  10. // 遍历这个二维数组
  11. foreach ($users as $user) {
  12. // $user 还是一个数组
  13. print_r($user);
  14. $table .= '<tr>';
  15. $table .= '<td>' . $user['id'] . '</td>';
  16. $table .= '<td>' . $user['name'] . '</td>';
  17. // 1->true, 0->false
  18. $table .= '<td>' . ($user['gender'] ? '女' : '男') . '</td>';
  19. $table .= '<td>' . $user['age'] . '</td>';
  20. $table .= '</tr>';
  21. }
  22. $table .= '</tbody></table>';
  23. echo $table;
用二维数组来模拟数据表查询结果集
  1. 代码重要部分,具体代码请看”heredoc,解析内部变量”,只适合少量的操作
    1. // 只查php
    2. if ($stu['course'] === 'php') {
    3. echo <<< STU <tr>
    4. <td>{$stu['id']}</td>
    5. <td>{$stu['name']}</td>
    6. <td class="active">{$stu['course']}</td>
    7. <td>{$stu['score']}</td>
    8. </tr>
    9. STU;
    10. }
  2. 代码重要部分,具体看”php模板语法”,专门用来写模板
    使用短标签进行简化,一般用于只打印一个变量操作:
    1. //原本写法
    2. <td><?php echo $stu['id'] ?></td>
    3. //简化写法,省略了 php echo
    4. <td><?= $stu['id'] ?></td>
    PHP模板写法 使用endforeach来表示结尾
    1. <?php foreach ($stus as $stu) : ?>
    2. //需要循环操作的代码
    3. <?php endforeach ?>

heredoc,解析内部变量

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

php模板语法

  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="zh-CN">
  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>php流程控制的模板语法/替代语法</title>
  23. <style>
  24. table {
  25. border-collapse: collapse;
  26. width: 360px;
  27. text-align: center;
  28. }
  29. table th,
  30. table td {
  31. border: 1px solid #000;
  32. padding: 5px;
  33. }
  34. table caption {
  35. font-size: 1.3em;
  36. }
  37. table thead {
  38. background-color: lightcyan;
  39. }
  40. .active {
  41. color: red;
  42. }
  43. </style>
  44. </head>
  45. <body>
  46. <table>
  47. <caption>学生成绩表</caption>
  48. <thead>
  49. <tr>
  50. <th>ID</th>
  51. <th>姓名</th>
  52. <th>课程</th>
  53. <th>成绩</th>
  54. </tr>
  55. </thead>
  56. <tbody>
  57. <!-- php模板语法的目标: html与php代码分离 -->
  58. <?php foreach ($stus as $stu) : ?>
  59. <!-- "{" => 冒号加php结束标记 -->
  60. <!-- 当前已离开了php环境,处于html中 -->
  61. <!-- 使用短标签进行简化: 只打印一个变量 -->
  62. <!-- <tr>
  63. <td><?php echo $stu['id'] ?></td>
  64. <td><?php echo $stu['name'] ?></td>
  65. <td><?= $stu['course'] ?></td>
  66. <td><?= $stu['score'] ?></td>
  67. </tr> -->
  68. <!-- 只输出成绩大于70分 -->
  69. <!-- <?php if ($stu['score'] > 70) : ?>
  70. <tr>
  71. <td><?php echo $stu['id'] ?></td>
  72. <td><?php echo $stu['name'] ?></td>
  73. <td><?= $stu['course'] ?></td>
  74. <td class="active"><?= $stu['score'] ?></td>
  75. </tr>
  76. <?php endif ?> -->
  77. <!-- 输出全部,并将不及格成绩描红 -->
  78. <tr>
  79. <td><?php echo $stu['id'] ?></td>
  80. <td><?php echo $stu['name'] ?></td>
  81. <td><?= $stu['course'] ?></td>
  82. <?php $active = $stu['score'] < 60 ? "active" : '' ?>
  83. <td class=<?= $active ?>><?= $stu['score'] ?></td>
  84. </tr>
  85. <!-- 动态设置样式的方法 -->
  86. <?php endforeach ?>
  87. </tbody>
  88. </table>
  89. </body>
  90. </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