Blogger Information
Blog 19
fans 0
comment 0
visits 9837
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
分支与循环示例及php模板与html混编示例
newbie
Original
399 people have browsed it

分支与循环

效果图

代码
  1. <?php
  2. //单分支
  3. $b = 7;
  4. if ($b < 6) {
  5. echo "b是一个小于6的整数";
  6. } else {
  7. echo "b不是一个小于6的整数";
  8. }
  9. echo "<hr>";
  10. $age = 18;
  11. //多分支判断
  12. if ($age < 18) {
  13. echo "小朋友请回家";
  14. } elseif ($age <= 30 && $age >= 18) {
  15. echo "欢迎收看";
  16. } elseif ($age <= 60 && $age >= 31) {
  17. echo "注意休息";
  18. } else {
  19. echo "年龄太大了 还是不要看了";
  20. }
  21. //循环语句
  22. $nums = [1, 2, 3, 4, 5];
  23. $ul = '<ul style="color: red;" >';
  24. $i = 0;
  25. while ($i < count($nums)) {
  26. $ul .= "<li>{$nums[$i]}</li>";
  27. $i++;
  28. }
  29. $ul .= "</ul>";
  30. echo $ul;
  31. echo "<hr>";
  32. // do while 循环 先执行再判断 无论对错先执行一次再进入判断
  33. $ul = '<ul style="color:#00ff80;">';
  34. $i = 0;
  35. do {
  36. $ul .= "<li>{$nums[$i]}</li>";
  37. $i++;
  38. } while ($i > count($nums));
  39. $ul .= '</ul>';
  40. echo $ul;
  41. echo "<hr>";
  42. // for 循环 中断与跳过
  43. $ul = '<ul style="color:#0000ff;">';
  44. for ($i = 0; $i < count($nums); $i++) {
  45. //跳过执行
  46. if ($i === 1) continue;
  47. $ul .= "<li>{$nums[$i]}</li>";
  48. }
  49. $ul .= '</ul>';
  50. echo $ul;
  51. echo "<hr>";
  52. // 中断执行
  53. $ul = '<ul style="color:#0000ff;">';
  54. for ($i = 0; $i < count($nums); $i++) {
  55. //跳过执行
  56. if ($i === 1) break;
  57. $ul .= "<li>{$nums[$i]}</li>";
  58. }
  59. $ul .= '</ul>';
  60. echo $ul;
  61. echo "<hr>";

php模板与html混编示例

效果图

代码
  1. <?php
  2. $nums = [1, 2, 3, 4, 5, 6, 7, 8];
  3. ?>
  4. <!DOCTYPE html>
  5. <html lang="zh-CN">
  6. <head>
  7. <meta charset="UTF-8">
  8. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  9. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  10. <title>php与html混编</title>
  11. <style>
  12. /* 写一个css样式 */
  13. .active {
  14. background-color: red;
  15. border: 1px solid pink;
  16. }
  17. </style>
  18. </head>
  19. <body>
  20. <ul>
  21. <?php foreach ($nums as $i) : ?>
  22. <!-- 输出索引大于3的数 -->
  23. <?php if ($nums[$i] > 3) : ?>
  24. <!-- 判断一下 给索引小于6的也就是5之前的数加一个样式 -->
  25. <?php $active = $nums[$i] < 6 ? "active" : ""; ?>
  26. <li class=<?= $active ?>><?= $nums[$i] ?></li>
  27. <?php endif ?>
  28. <?php endforeach ?>
  29. </ul>
  30. </body>
  31. </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