Blogger Information
Blog 26
fans 0
comment 3
visits 17759
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php运算符,循环分支,文件包含探究
后网络时代
Original
637 people have browsed it

1. 对象课件,将提及的全部运算符上机做一遍,并将你认为重要的发到博客中;

  1. <?php
  2. //php运算符号
  3. function powexc($a,$b){
  4. if (is_numeric($a) && is_numeric($b)) :
  5. if (version_compare(PHP_VERSION, '5.6.0') > 0) :
  6. return $a**$b;
  7. else :
  8. return pow($a, $b);
  9. endif;
  10. else:
  11. return '参数错误,请求拒绝!';
  12. endif;
  13. }
  14. echo '幂运算:10的平方:', powexc(10,2);
  15. echo "<pre></pre>";
  16. //太空船运算符;
  17. function comparerun(){
  18. //null 合并运算符号
  19. $a=$_GET['c1']??1;
  20. $b=$_GET['c2']??1;
  21. return $a<=>$b;
  22. }
  23. switch(comparerun()){
  24. case 0:
  25. echo "两个数相等";
  26. break;
  27. case 1:
  28. echo "第一个数大于第二个数";
  29. break;
  30. case -1;
  31. echo "第一个数小于第二个数";
  32. break;
  33. default:
  34. echo '比较拒绝!';
  35. }
  36. echo "<pre></pre>";
  37. //求摸运算符
  38. $year=2020;
  39. if($year%4==0 && $year%100!=0){
  40. echo $year."是闰年.";
  41. }

效果:

2. 实例演示分支与循环结构 ,并熟练写出他们的替代语法(不得抄课堂代码),自己想。

  1. <?php
  2. //单分支
  3. if (!isset($num)) {
  4. echo "未赋值!";
  5. }
  6. echo "<pre></pre>";
  7. if (!isset($num)) :
  8. echo "未赋值!";
  9. endif;
  10. echo "<pre></pre>";
  11. //双分支
  12. $flag = 0;
  13. if (!isset($flag)) {
  14. echo "未赋值!";
  15. } else {
  16. echo "已赋值!";
  17. }
  18. echo "<pre></pre>";
  19. if (!isset($flag)) :
  20. echo "未赋值!";
  21. else :
  22. echo "已赋值!";
  23. endif;
  24. echo "<pre></pre>";
  25. //多分枝
  26. $age = $age ?? 17;
  27. if ($age <= 6) {
  28. echo '婴幼儿';
  29. } else if ($age <= 12) {
  30. echo '少儿';
  31. } else if ($age <= 17) {
  32. echo '青少年';
  33. } else if ($age <= 45) {
  34. echo '青年';
  35. } else if ($age <= 69) {
  36. echo '中年';
  37. } else {
  38. echo '老年';
  39. }
  40. echo "<pre></pre>";
  41. if ($age <= 6) :
  42. echo '婴幼儿';
  43. elseif ($age <= 12) :
  44. echo '少儿';
  45. elseif ($age <= 17) :
  46. echo '青少年';
  47. elseif ($age <= 45) :
  48. echo '青年';
  49. elseif ($age <= 69) :
  50. echo '中年';
  51. else :
  52. echo '老年';
  53. endif;
  54. echo "<pre></pre>";
  55. $age = 45;
  56. function returnage()
  57. {
  58. global $age;
  59. if ($age <= 6) :
  60. return '婴幼儿';
  61. elseif ($age <= 12) :
  62. return '少儿';
  63. elseif ($age <= 17) :
  64. return '青少年';
  65. elseif ($age <= 45) :
  66. return '青年';
  67. elseif ($age <= 69) :
  68. return '中年';
  69. else :
  70. return '老年';
  71. endif;
  72. }
  73. switch (returnage()) {
  74. case '婴幼儿':
  75. echo '幼稚园阶段';
  76. break;
  77. case '少儿':
  78. echo '小学教育';
  79. break;
  80. case '青少年':
  81. echo '初高中阶段';
  82. break;
  83. case '青年':
  84. echo '社会的中流砥柱';
  85. break;
  86. case '中年':
  87. echo '上有老下有小';
  88. break;
  89. case '老年';
  90. echo '退休养老';
  91. break;
  92. default:
  93. echo "人生轨迹不明";
  94. }
  95. echo "<pre></pre>";
  96. switch (returnage()):
  97. case '婴幼儿':
  98. echo '幼稚园阶段';
  99. break;
  100. case '少儿':
  101. echo '小学教育';
  102. break;
  103. case '青少年':
  104. echo '初高中阶段';
  105. break;
  106. case '青年':
  107. echo '社会的中流砥柱';
  108. break;
  109. case '中年':
  110. echo '上有老下有小';
  111. break;
  112. case '老年';
  113. echo '退休养老';
  114. break;
  115. default:
  116. echo "人生轨迹不明";
  117. endswitch;
  118. echo "<pre></pre>";
  119. echo "循环语句:";
  120. echo "<pre></pre>";
  121. foreach($_SERVER as $key => $v){
  122. echo "<pre> {$key} => {$v}</pre>";
  123. }
  124. echo "<pre></pre>";
  125. foreach($_SERVER as $key => $v):
  126. echo "<pre> {$key} => {$v}</pre>";
  127. endforeach;
  128. echo "<pre></pre>";
  129. for($i=0;;$i++){
  130. if($i==5){
  131. break;
  132. }
  133. echo str_replace("\n","<br>",$i."\n") ;
  134. }
  135. echo "<pre></pre>";
  136. for($i=0;;$i++):
  137. if($i==5):
  138. break;
  139. endif;
  140. echo str_replace("\n","<br>",$i."\n") ;
  141. endfor;
  142. echo "<pre></pre>";
  143. $flag=0;
  144. while(!$flag){
  145. echo "条件成立才执行";
  146. $flag=1;
  147. }
  148. echo "<pre></pre>";
  149. $flag=0;
  150. while(!$flag):
  151. echo "条件成立才执行";
  152. $flag=1;
  153. endwhile;
  154. $flag=0;
  155. echo "<pre></pre>";
  156. do{
  157. echo "至少执行一次";
  158. $flag=1;
  159. }while(!$flag);
  160. echo "<pre></pre>";

效果:

3. 文件包含的本质是什么?include,require的区别是什么,用在什么地方?

答:
文件包含本质:模块化编程.
include包含文件时候,若果文件不存在,只是警告一下,后续语句会继续执行,require抛出严重错误,后续语句不再继续执行。
include和require都可以返回值不是真的函数,include在运行阶段执行,可以放在if 和函数中。
require 和 include 几乎完全一样,除了处理失败的方式不同之外。
containfile.php

  1. <?php
  2. //
  3. require'config.php';
  4. echo $config;
  5. echo "<br>";
  6. $a=include 'returnarray.php';
  7. $b=include 'returnarray.php';
  8. echo $a===$b;
  9. echo "<br>";
  10. function houxuzhixing(){
  11. require'config.php'; //包含的变量作用尽在此时
  12. }

config.php

  1. <?php
  2. $config="hello,文件包含";
  3. class test{
  4. function __construct()
  5. {
  6. $c= include 'returnarray.php';
  7. echo "okay";
  8. }
  9. }
  10. new test();

returnarray.php

  1. <?php
  2. return ['hello','word'];

效果:

Correcting teacher:天蓬老师天蓬老师

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