Blogger Information
Blog 13
fans 0
comment 0
visits 9619
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
isset()与empty()函数 switch语句改写简易计算器
樱风醉
Original
683 people have browsed it

一.isset()与 empty()函数

isset()函数

  • isset()函数中0、false、''都认为它们是有值的,返回值是 true
  • null 和 没有这个变量,返回值是 false
       
  1. $null=null;
  2. $num=0;
  3. $str='';
  4. $bool=false;
  5. var_dump(isset($num));// true
  6. echo'
    '
    ;
  7. var_dump(isset($str));// true
  8. echo'
    '
    ;
  9. var_dump(isset($bool));// true
  10. echo'
    '
    ;
  11. var_dump(isset($null));// false
  12. echo'
    '
    ;
  13. var_dump(isset($no));//false

empty()函数

  • empty()函数判断变量为空的情况时会返回 true
  • 空字符串'' 数字零0 bool值false null 字符串零'0' 或没有这个变量,都为空,返回值为 true
       
  1. $str='';
  2. $num=0;
  3. $bool=false;
  4. $null=null;
  5. $str0='0';
  6. var_dump(empty($str));// true
  7. echo'
    '
    ;
  8. var_dump(empty($num));// true
  9. echo'
    '
    ;
  10. var_dump(empty($bool));// true
  11. echo'
    '
    ;
  12. var_dump(empty($null));// true
  13. echo'
    '
    ;
  14. var_dump(empty($str0));// true
  15. echo'
    '
    ;
  16. var_dump(empty($no));// true

二.switch 语句

  • 用于根据多个不同条件执行不同动作
       
  1. switch(n)
  2. {
  3. caselabel1:
  4. 如果n=label1,此处代码将执行;
  5. break;
  6. caselabel2:
  7. 如果n=label2,此处代码将执行;
  8. break;
  9. default:
  10. 如果n既不等于label1也不等于label2,此处代码将执行;
  11. }

三.switch 语句改写计算器

       
  1. lang="zh-CN">
  2. charset="UTF-8">
  3. http-equiv="X-UA-Compatible"content="IE=edge">
  4. name="viewport"content="width=device-width, initial-scale=1.0">
  5. </span><span class="pln">简易计算器</span><span class="tag">
  6. 简易计算器

  7. action=""method="get">
  8. isset($_GET['num1'])?$_GET['num1']:'';?>">
  9. name="opt"id="">
  10. isset($_GET['num2'])?$_GET['num2']:'';?>">
  11. type="submit"value="计算">
  12. php
  13. if(!empty($_GET)){
  14. $opt=$_GET['opt'];
  15. $num1=$_GET['num1'];
  16. $num2=$_GET['num2'];
  17. switch($opt){
  18. case'+':
  19. $num=$num1+$num2;
  20. echo'计算结果:'.$num1.'+'.$num2.'='.$num;
  21. break;
  22. case'-':
  23. $num=$num1-$num2;
  24. echo'计算结果:'.$num1.'-'.$num2.'='.$num;
  25. break;
  26. case'*':
  27. $num=$num1*$num2;
  28. echo'计算结果:'.$num1.'×'.$num2.'='.$num;
  29. break;
  30. case'/':
  31. if($num2==0){
  32. echo'除数不能为0';
  33. }else{
  34. $num=$num1/$num2;
  35. echo'计算结果:'.$num1.'÷'.$num2.'='.$num;
  36. }
  37. break;
  38. case'%':
  39. if($num2==0){
  40. echo'除数不能为0';
  41. }else{
  42. $num=$num1%$num2;
  43. echo'计算结果:'.$num1.'%'.$num2.'='.$num;
  44. }
  45. break;
  46. }
  47. }
  48. ?>

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 withNews Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!