Blogger Information
Blog 33
fans 0
comment 0
visits 18751
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
分支、循环、php模板语法与html混编实例演示
李玉峰
Original
363 people have browsed it

分支实例演示

  1. <?php
  2. // 流程控制:分支
  3. // 1.单分支
  4. $age = 10;
  5. if($age >= 18){
  6. echo '恭喜你,成年人';
  7. }
  8. // 2.双分支
  9. $age = 10;
  10. if($age >= 18){
  11. echo '恭喜你,成年人';
  12. }else{
  13. echo '小学生,好好学习';
  14. }
  15. echo '<hr>';
  16. // 分支只有一行代码,可以不写大括号
  17. $age = 30;
  18. if($age >= 18)
  19. echo '恭喜你,成年人';
  20. else
  21. echo '小学生,好好学习';
  22. echo '<hr>';
  23. // 3.多分支
  24. $age = 100;
  25. if($age >= 18 && $age < 40 ){
  26. echo "{$age}岁,改结婚了";
  27. }else if($age >= 40 ) {
  28. echo "{$age}岁,好好锻炼身体";
  29. }else{
  30. echo '小学生,好好学习';
  31. }
  32. echo '<hr>';
  33. // 4.多分支的语法糖:switch
  34. $age = 25;
  35. switch(true)
  36. {
  37. case $age >= 18 && $age < 40 ;
  38. echo "{$age}岁,改结婚了";
  39. break;
  40. case $age >= 40 ;
  41. echo "{$age}岁,好好锻炼身体";
  42. break;
  43. default:
  44. echo '小学生,好好学习';
  45. }

循环实例演示

  1. <?php
  2. // 流程控制:循环
  3. $car = ['BMW','volvo','haval'];
  4. print_r($car);
  5. echo '<br>';
  6. echo $car[2] . '<br>';
  7. // 1.初始化循环变量,用索引当循环变量
  8. $i =0;
  9. // 2.循环条件:
  10. // 数组长度 = 最大索引+1
  11. // echo count($car);
  12. if($i < count($car))
  13. {
  14. echo $car[$i] . '<br>';
  15. }
  16. // 3.更新循环条件
  17. $i =$i+1;
  18. if($i < count($car))
  19. {
  20. echo $car[$i] . '<br>';
  21. }
  22. echo '<hr>';
  23. // 简化循环改写:while()
  24. $i = 0;
  25. while($i < count($car))
  26. {
  27. echo $car[$i] . '<br>';
  28. $i =$i+1;
  29. }
  30. // 案例列表
  31. $list = '<ul style="border:1px solid;background:cyan;">';
  32. $i = 0;
  33. while($i < count($car))
  34. {
  35. $list .= "<li>{$car[$i]}</li>";
  36. $i =$i+1;
  37. }
  38. $list .= '</ul>';
  39. echo $list;
  40. echo '<hr>';
  41. $list = '<ul style="border:1px solid;background:red;">';
  42. $i = 0;
  43. do{
  44. $list .= "<li>{$car[$i]}</li>";
  45. $i =$i+1;
  46. }while($i > count($car));
  47. $list .= '</ul>';
  48. echo $list;
  49. // for改写
  50. $list = '<ul style="border:1px solid;background:yellow;">';
  51. for($i =0;$i < count($car);$i++)
  52. {
  53. $list .= "<li>{$car[$i]}</li>";
  54. }
  55. $list .= '</ul>';
  56. echo $list;
  57. // 中断:break
  58. $list = '<ul style="border:1px solid;background:violet;">';
  59. for($i =0;$i < count($car);$i++)
  60. {
  61. if($i >1) break;
  62. $list .= "<li>{$car[$i]}</li>";
  63. }
  64. $list .= '</ul>';
  65. echo $list;
  66. // 跳过某次循环:continue;
  67. $list = '<ul style="border:1px solid;background:violet;">';
  68. for($i =0;$i < count($car);$i++)
  69. {
  70. if($i === 1) continue;
  71. $list .= "<li>{$car[$i]}</li>";
  72. }
  73. $list .= '</ul>';
  74. echo $list;

php模板语法与html混编实例演示

  1. <?php
  2. $users =[
  3. ['id'=>'1','name'=>'李白','course' =>'php','gender'=>0,'score'=>60],
  4. ['id'=>'2','name'=>'杜甫','course' =>'mysql','gender'=>1,'score'=>30],
  5. ['id'=>'3','name'=>'韩愈','course' =>'js','gender'=>0,'score'=>90],
  6. ['id'=>'4','name'=>'杜牧','course' =>'php','gender'=>1,'score'=>80],
  7. ];
  8. ?>
  9. <!DOCTYPE html>
  10. <html lang="zh-CN">
  11. <head>
  12. <meta charset="UTF-8">
  13. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  14. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  15. <title>php模板语法</title>
  16. </head>
  17. <body>
  18. <table border="1" width ="500" cellspacing ="0" cellpadding="3" aling ="center">
  19. <caption>用户信息表</caption>
  20. <thead>
  21. <tr bgcolor = "#ccc">
  22. <th>id</th>
  23. <th>姓名</th>
  24. <th>课程</th>
  25. <th>性别</th>
  26. <th>成绩</th>
  27. </tr>
  28. </thead>
  29. <!-- 遍历 -->
  30. <tbody align="center">
  31. <?php foreach($users as $user) : ?>
  32. <!-- “{” => 冒号加php结束标记 -->
  33. <!-- <tr>
  34. <td><?php echo $user['id'] ?></td>
  35. <td><?php echo $user['name'] ?></td>
  36. <td><?php echo $user['course'] ?></td>
  37. <td><?php echo $user['gender']?'女' : '男' ?></td>
  38. <td><?php echo $user['score'] ?></td>
  39. </tr> -->
  40. <!-- 仅输出成绩大于60分的 -->
  41. <!-- <?php if ($user['score'] >= 60) : ?>
  42. <tr>
  43. <td><?php echo $user['id'] ?></td>
  44. <td><?php echo $user['name'] ?></td>
  45. <td><?php echo $user['course'] ?></td>
  46. <td><?php echo $user['gender']?'女' : '男' ?></td>
  47. <td><?php echo $user['score'] ?></td>
  48. </tr>
  49. <?php endif ?> -->
  50. <!-- 输出全部,低于60分的标红 -->
  51. <tr>
  52. <td><?php echo $user['id'] ?></td>
  53. <td><?php echo $user['name'] ?></td>
  54. <td><?php echo $user['course'] ?></td>
  55. <td><?php echo $user['gender']?'女' : '男' ?></td>
  56. <?php $color = $user['score'] < 60 ?'style="color: red;"' : '' ?>
  57. <td <?php echo $color ?>><?php echo $user['score'] ?></td>
  58. </tr>
  59. <?php endforeach ?>
  60. </tbody>
  61. </table>
  62. </body>
  63. </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