Blogger Information
Blog 31
fans 2
comment 0
visits 27687
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
选择分支及循环语句
霏梦
Original
922 people have browsed it

- 作者:霏梦

  • 代码区
  1. <?php
  2. // 1、选择判断
  3. $salary=10000;
  4. $year=5;
  5. $adjust_salary=$salary;
  6. // 单分支
  7. if($year<=5){
  8. $adjust_salary=$salary * 0.5;
  9. }
  10. echo '你可以拿到'.$adjust_salary.'工资<br>';
  11. // 双分支
  12. $salary=10000;
  13. $year=6;
  14. $adjust_salary=$salary;
  15. if($year<=5){
  16. $adjust_salary=$salary * 0.5;
  17. }else{
  18. $adjust_salary=$salary * 0.7;
  19. }
  20. echo '你可以拿到'.$adjust_salary.'工资<br>';
  21. // 多分支
  22. $salary=10000;
  23. $year=11;
  24. $adjust_salary=$salary;
  25. if($year<=5){
  26. $adjust_salary=$salary * 0.5;
  27. }elseif($year>5 && $year <=10){
  28. $adjust_salary=$salary * 0.7;
  29. }elseif($year>10 && $year <=20){
  30. $adjust_salary=$salary * 0.9;
  31. }else{
  32. $adjust_salary=$salary;
  33. }
  34. echo '你可以拿到'.$adjust_salary.'工资<br>';
  35. // 简化多分支
  36. $salary=10000;
  37. $year=8;
  38. $adjust_salary=$salary;
  39. switch($year){
  40. case $year <=5:
  41. $adjust_salary=$salary * 0.5;
  42. break;
  43. case $year>5 && $year <=10:
  44. $adjust_salary=$salary * 0.7;
  45. break;
  46. case $year>10 && $year <=20:
  47. $adjust_salary=$salary * 0.9;
  48. break;
  49. default:
  50. $adjust_salary=$salary * 0.9;
  51. }
  52. echo '你可以拿到'.$adjust_salary.'工资<br>';
  53. // switch主要用在单值判断中
  54. $salary=10000;
  55. $year=8;
  56. $adjust_salary=$salary;
  57. switch($year){
  58. case 6:
  59. echo '我上班6年了';
  60. break;
  61. case 7:
  62. echo "我7上班年了";
  63. break;
  64. case 8:
  65. echo "我上班8年了";
  66. break;
  67. default:
  68. echo "我还没有上班";
  69. }
  70. echo '<hr>';
  71. // 2 循环
  72. // while,先判断,为真,就执行
  73. $num=1;
  74. while($num<=10){
  75. echo '$num的值分别是:'.$num.'<br>';
  76. $num++;
  77. }
  78. echo '<hr>';
  79. //do while 先执行,再判断
  80. $num=1;
  81. do{
  82. echo '$num的值分别是:'.$num.'<br>';
  83. $num++;
  84. }while($num<=10);
  85. echo "<hr>";
  86. // for循环初始值,条件,条件增量
  87. for($num=0;$num<=10;$num++){
  88. // if($num>8)
  89. // {
  90. // // exit();
  91. // // break后面的语句不在执行
  92. // break;
  93. // }
  94. if($num==5){
  95. continue;
  96. }
  97. echo '$num的值分别是:'.$num.'<br>';
  98. }
  99. $person=[
  100. ['id'=>1,'name'=>'meng1','sex'=>'man'],
  101. ['id'=>2,'name'=>'meng2','sex'=>'woman'],
  102. ['id'=>3,'name'=>'meng3','sex'=>'--'],];
  103. ?>
  104. <!DOCTYPE html>
  105. <html lang="en">
  106. <head>
  107. <meta charset="UTF-8">
  108. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  109. <title>Document</title>
  110. </head>
  111. <body>
  112. <table border="1" cellspacing='1'>
  113. <tr>
  114. <td>id</td>
  115. <td>姓名</td>
  116. <td>性别</td>
  117. </tr>
  118. <?php for($i=0; $i<count($person);$i++):?>
  119. <tr>
  120. <td width='100'><?php echo $person[$i]['id'] ?></td>
  121. <td width='100'><?php echo $person[$i]['name'] ?></td>
  122. <td width='100'><?php echo $person[$i]['sex'] ?></td>
  123. </tr>
  124. <?php endfor ?>
  125. </table>
  126. </body>
  127. </html>
  • 效果区

选择分支及循环

Correcting teacher:GuanhuiGuanhui

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