Blogger Information
Blog 145
fans 7
comment 7
visits 164670
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP基础知识:常见运算符、语句分支(模板写法)和文档导入
李东亚¹⁸⁰³⁹⁵⁴⁰¹²⁰
Original
946 people have browsed it

PHP常见运算符

1.算术运算符:

  • 加减乘除:+、-、*、/
  • 预算赋值:++--;先加(减)再用,先用后加(减)
  • 次方运算:**;例如:1的三次方1**3
  • 取余运算:%;取两数相除的余数

2.字符串运算符:.字符传连接符

3.赋值运算符:

  • 先运算后赋值:+=、-=、*=、/=、%=、.=

4.比较运算(返回布尔值)

  • 常见的比较运算符:=、>=、<=、==(值相等)、===(全等)、!=(不相等)、!==(不全等)
  • 太空运算符:<=>;例如:$a<=>$b,如果$a>$b返回大于0的整数,如果$a=$b返回等于0的整数,如果$a<$b返回小于0的整数;

PHP语句分支

1.分支语句:

  • if(){}:单分支语句
  • if(){……}else{……}:双分支语句;双分支语句(三元表达式简写):判单条件?"返回值1":"返回值2"
  • if(){……}elseif{……}……else{……}:多分支语句;switch($value){case 值:……break;…………default:……}

2.循环语句:

  • while循环:

    • 入口判断:初始条件;while(判断条件){语句;循环条件}
    • 出口判断:初始条件;do{语句;循环条件}while(判断条件)
  • for循环:for(初始条件;判断条件;循环条件){语句}

  1. foreach($arr as $item){
  2. //遍历数组中的值
  3. echo $item;
  4. }

3.语句分支的模板写法:

  • 去掉大括号{},用冒号代替:
  • 结束时用end+语句分支关键字即可;
  1. if($a>5):
  2. echo $a;
  3. elseif($a<10):
  4. echo ++$a;
  5. endif;
  6. switch($a):
  7. case 5:
  8. echo $a;
  9. break;
  10. default:
  11. echo $a+$b;
  12. endswitch;
  13. echo "<br>";
  14. $arr=["one","two","three"];
  15. for($i=0;$i<count($arr);$i++):
  16. echo $arr[$i]."<br>";
  17. endfor;
  18. foreach($arr as $item):
  19. echo $item;
  20. endforeach;

PHP文档导入

  • require(_once):导入文档,当出现错误时中止代码(仅导入一次,不重复导入)
  • include(_once):导入文档,当出现错误还继续执行后面代码(进导入一次,不重复导入);include导入文档时在运行阶段导入,可以在if语句和函数中使用

代码练习

  1. <?php
  2. $a=10;
  3. $b=6;
  4. echo "/取商运算:",$a/$b,'<br>';
  5. echo "%取余运算:",$a%$b,'<br>';
  6. echo "<br>";
  7. echo "round取四舍五入取整运算:",round($a/$b),'<br>';
  8. echo "ceil向上取整运算:",ceil($a/$b),'<br>';
  9. echo "floor向下取整运算:",floor($a/$b),'<br>';
  10. echo "intdiv取整相除运算:",intdiv($a,$b),'<br>';
  11. echo $a**$b;
  12. echo "<br>";
  13. echo ++$a;
  14. echo $a;
  15. echo "<br>";
  16. echo $a--;
  17. echo $a;
  18. echo "太空运算符";
  19. var_export($a<=>$b);
  20. echo "###################";
  21. echo "<br>";
  22. if($a>5):
  23. echo $a;
  24. elseif($a<10):
  25. echo ++$a;
  26. endif;
  27. switch($a):
  28. case 5:
  29. echo $a;
  30. break;
  31. default:
  32. echo $a+$b;
  33. endswitch;
  34. echo "<br>";
  35. $arr=["one","two","three"];
  36. for($i=0;$i<count($arr);$i++):
  37. echo $arr[$i]."<br>";
  38. endfor;
  39. foreach($arr as $item):
  40. echo $item;
  41. endforeach;
  42. require "demo.php";//导入文档内容,
  43. include "demo1.php";
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