Blogger Information
Blog 41
fans 0
comment 0
visits 31025
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP运算符、分支及循环
陈强
Original
684 people have browsed it

运算符

算术运算符

  • “+”,”-“,”*”,”/“ 加减乘除
  1. $num1 = 4;
  2. $num2 = 2;
  3. $num1+$num2 //输出6,相当于4+2
  4. $num1-$num2 //输出2,相当于4-2
  5. $num1*$num2 //输出8,相当于4*2
  6. $num1/$num2 //输出2,相当于4/2
  • “%” 取模运算
  1. $num1 = 3;
  2. $num2 = 2;
  3. $num1+$num2 //输出1,相当于3/2,取到余数1
  • “++”,”—“自增自减运算
  1. $num1 = 4;
  2. $num2 = $num1++; //等同于$num2 = $num; $num1+1
  3. //输出$num1=5,$num2=4
  4. $num2 = ++$num1; //等同于$num1+1;$num2 = $num1
  5. //输出$num1=5,$num2=5
  6. $num1 = 4;
  7. $num2 = $num1--; //等同于$num2 = $num; $num1-1
  8. //输出$num1=3,$num2=4
  9. $num2 = ++$num1; //等同于$num1-1;$num2 = $num1
  10. //输出$num1=3,$num2=3
  • 幂运算使用运算符’**’,需要php5.6以上版本
  1. $num1 = 4**2;// 输出16,计算4的2次方

字符串运算符

  • 字符串运算符用”.”连接
  1. $str1 = 'Hello';
  2. $str2 = 'World';
  3. echo $str1.$str2;//输出 Hello World

赋值运算符

  • 赋值使用 ‘=’
  1. $num = 4;//将4赋值给$num1
  2. $num += 4;//$num = $num+4
  3. $num -= 4;//$num = $num-4
  4. $num *= 4;//$num = $num*4
  5. $num /= 4;//$num = $num/4
  6. $str1 = 'Hello';
  7. $str1 .= 'World';//输出Hello World 等同于 $str1= $str1.'World'

比较运算符

比较运算符: 返回布尔值,常用于流程控制中

  • ‘<’,’>’,’>=’,’<=’
  1. $num1 = 3;
  2. $num2 = 2;
  3. $num1 > $num2 //输出1 ,表示true
  4. $num1 >= $num2 //输出1 ,表示true
  5. $num1 < $num2 //无输出,表示false
  6. $num1 <= $num2 //无输出,表示false
  • ‘==’,’===’,’!=’,’!==’

    ‘==’ 要求两边值相等;’===’要求两边值相等并且类型相同

  1. $num1 = 3;
  2. $num2 = '3';
  3. $num1 == $num2 //输出1 ,表示true
  4. $num1 === $num2 //输出0 ,false
  5. $num1 != $num2 //无输出,表示false
  6. $num1 !== $num2 //输出1 ,表示true
  • <=>太空船 php7.1以上版本试用
  1. $num1 = 3;
  2. $num2 = 5;
  3. echo $num1<=>$num2 //输出-1
  4. //如果$num1>$num2 输出大于0的整数
  5. //如果$num1<$num2 输出小于0的整数
  6. //如果$num1=$num2 输出等于0的整数

分支流程控制

  • 单分支 if
  1. $num1 = 5;
  2. if ($num1 > 3) {
  3. echo $num1, '大于3';
  4. }
  5. 简化版:去掉大括号
  6. if($num1 > 3):
  7. echo $num1, '大于3';
  8. endif;
  • 多分支 if … elseif …else
  1. $num1 = 5;
  2. if ($num1 > 5) {
  3. echo $num1, '大于5';
  4. } elseif($num1<5) {
  5. echo $num1,'小于5';
  6. }else {
  7. echo $num1,'等于5';
  8. }
  9. 简化版:去掉大括号
  10. if ($num1 > 5) :
  11. echo $num1, '大于5';
  12. elseif($num1<5) :
  13. echo $num1,'小于5';
  14. else :
  15. echo $num1,'等于5';
  16. endif;
  • swicth 语句
  1. $num1 = 5;
  2. switch ($num1) {
  3. case $num1 > 5:
  4. echo $num1, '大于5';
  5. break;
  6. case $num1 < 5:
  7. echo $num1, '小于5';
  8. break;
  9. default:
  10. echo $num1, '等于5';
  11. }
  12. 简化版:去掉大括号
  13. switch ($num1):
  14. case $num1 > 5:
  15. echo $num1, '大于5';
  16. break;
  17. case $num1 < 5:
  18. echo $num1, '小于5';
  19. break;
  20. default:
  21. echo $num1, '等于5';
  22. endswitch;

循环

  • 入口判断型 while
  1. $nums = [1, 2, 3, 4, 5];
  2. $i = 0;
  3. while ($i < count($nums)) {
  4. echo $nums[$i];
  5. $i++;
  6. }
  7. 简化版:去掉大括号
  8. while ($i < count($nums)) :
  9. echo $nums[$i];
  10. $i++;
  11. endwhile;
  • 出口判断型 do…while :如果不满足条件至少会循环一次
  1. $i = 0;
  2. do {
  3. echo $nums[$i];
  4. $i++;
  5. } while ($i > count($nums));
  6. 简化版:去掉大括号
  7. 暂无

  • for循环
  1. $nums = [1, 2, 3, 4, 5];
  2. for ($i = 0; $i < count($nums); $i++) {
  3. echo $num[$i];
  4. }
  5. 简化版:去掉大括号
  6. for ($i = 0; $i < count($nums); $i++) :
  7. echo $num[$i];
  8. endfor;
  • break 与 continue
  1. $nums = [1, 2, 3, 4, 5];
  2. $i = 0;
  3. do {
  4. if ($i > 3) break;
  5. echo $nums[$i];
  6. $i++;
  7. } while ($i < count($nums));

  1. $nums = [1, 2, 3, 4, 5];
  2. for ($i = 0; $i < count($nums); $i++) :
  3. if ($i === 3) continue;
  4. echo $nums[$i];
  5. endfor;

include 与 require

  • include是在运行阶段包含,所以可以用在if中;require不可以
  • include运行出错,程序可以继续执行;require不可以
  • include_once,require_once只包含一次
  • require= include…eixt;
  1. //引入文件的方式
  2. include 'header.php';
  3. require 'header.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