Blogger Information
Blog 47
fans 0
comment 0
visits 21007
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
分支,循环,流程控制
P粉036614676
Original
271 people have browsed it
  1. 实例演示分支与循环, 不要抄源码 2. 实例演示流程控制之模板语法, 自己测试switch

    1.分支

    1.1分支

    1. if($count > 0)
    2. {
    3. if($count == 1)
    4. {
    5. echo '1' . PHP_EOL;
    6. }
    7. elseif ($count == 2)
    8. {
    9. echo '2' . PHP_EOL;
    10. }
    11. else
    12. {
    13. echo '3' . PHP_EOL;
    14. }
    15. }

    1.2语法糖

    目标:判断大人小孩
    1. $age = 20;
    2. $person = $age > 18 ? '大人' : '小孩';
    3. echo $person . PHP_EOL;

2.循环

2.1switch循环

目标:判断是否及格

  1. $m = 100;
  2. switch ($m)
  3. {
  4. case $m<60:
  5. echo '不及格';
  6. break;
  7. default:
  8. echo '及格';
  9. }

2.2 while循环

目标:输出0-9

  1. $y = 10;
  2. do{
  3. echo $GLOBALS['y'];
  4. static $count = 0;
  5. echo $count . " ";
  6. $count++;
  7. }while($count < 10);

2.3 for循环

目标:屏幕上输出颜色

  1. $color = ['red','blue','green'];
  2. $list = '<ul style="border:1px solid red ;">';
  3. for($i = 0;$i < count($color);$i++)
  4. {
  5. $list .= "<li>$color[$i]</li>";
  6. }
  7. $list .= '</ul>';
  8. echo $list;

2.4 foreach循环

目标:遍历数组

  1. $person = ['name' => 'yk','wight' => 60];
  2. echo $person;
  3. foreach ($person as $key => $value)
  4. {
  5. echo $key . ' ' . $value;
  6. }

3.模板语法

3.1普通写法

  1. <?php
  2. $users = [
  3. ['1','吴昊','0',22],
  4. ['2','吴啊','1',23],
  5. ['3','爱昊','0',25],
  6. ]
  7. ?>
  8. <!DOCTYPE html>
  9. <html lang="en">
  10. <head>
  11. <meta charset="UTF-8">
  12. </head>
  13. <style>
  14. th,td{
  15. border: 1px solid gray;
  16. margin: 0;
  17. }
  18. </style>
  19. <body>
  20. <table style="border: 1px solid green;">
  21. <caption>学生体检表</caption>
  22. <thead>
  23. <tr>
  24. <th>ID</th>
  25. <th>姓名</th>
  26. <th>性别</th>
  27. <th>年龄</th>
  28. </tr>
  29. </thead>
  30. <tbody>
  31. <?php
  32. foreach ($users as $user)
  33. {
  34. echo '<tr>';
  35. echo "<td>$user[0]</td>";
  36. echo "<td>$user[1]</td>";
  37. $sex = $user[2] ? '男' : '女';
  38. echo '<td>'. $sex .'</td>';
  39. echo "<td>$user[3]</td>";
  40. echo '</tr>';
  41. }
  42. ?>
  43. </tbody>
  44. </table>
  45. </body>
  46. </html>

3.2模板写法

  1. <?php
  2. $users = [
  3. ['1','吴昊','0',22],
  4. ['2','吴啊','1',23],
  5. ['3','爱昊','0',25],
  6. ]
  7. ?>
  8. <!DOCTYPE html>
  9. <html lang="en">
  10. <head>
  11. <meta charset="UTF-8">
  12. </head>
  13. <style>
  14. th,td{
  15. border: 1px solid gray;
  16. margin: 0;
  17. }
  18. </style>
  19. <body>
  20. <table style="border: 1px solid green;">
  21. <caption>学生体检表</caption>
  22. <thead>
  23. <tr>
  24. <th>ID</th>
  25. <th>姓名</th>
  26. <th>性别</th>
  27. <th>年龄</th>
  28. </tr>
  29. </thead>
  30. <tbody>
  31. <?php
  32. foreach ($users as $user):
  33. ?>
  34. <tr>
  35. <td><?=$user[0]?></td>
  36. <td><?=$user[1]?></td>
  37. <?php
  38. $sex = $user[2] ? '男' : '女';
  39. ?>
  40. <td><?=$sex?></td>
  41. <td><?=$user[3]?></td>
  42. </tr>
  43. <?php
  44. endforeach;
  45. ?>
  46. </tbody>
  47. </table>
  48. </body>
  49. </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