Blogger Information
Blog 13
fans 0
comment 0
visits 9682
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
isset()与empty()函数 switch语句改写简易计算器
樱风醉
Original
693 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 '<br>';
  7. var_dump(isset($str)); // true
  8. echo '<br>';
  9. var_dump(isset($bool)); // true
  10. echo '<br>';
  11. var_dump(isset($null)); // false
  12. echo '<br>';
  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 '<br>';
  8. var_dump(empty($num)); // true
  9. echo '<br>';
  10. var_dump(empty($bool)); // true
  11. echo '<br>';
  12. var_dump(empty($null)); // true
  13. echo '<br>';
  14. var_dump(empty($str0)); // true
  15. echo '<br>';
  16. var_dump(empty($no)); // true

二.switch 语句

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

三.switch 语句改写计算器

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>简易计算器</title>
  8. </head>
  9. <body>
  10. <h2>简易计算器</h2>
  11. <form action="" method="get">
  12. <input type="number" name="num1" value="<?= isset($_GET['num1']) ? $_GET['num1'] : ''; ?>">
  13. <select name="opt" id="">
  14. <option value="+" <?= isset($_GET['opt']) && $_GET['opt'] == '+' ? 'selected' : ''; ?>>+</option>
  15. <option value="-" <?= isset($_GET['opt']) && $_GET['opt'] == '-' ? 'selected' : ''; ?>>-</option>
  16. <option value="*" <?= isset($_GET['opt']) && $_GET['opt'] == '*' ? 'selected' : ''; ?></option>
  17. <option value="/" <?= isset($_GET['opt']) && $_GET['opt'] == '/' ? 'selected' : ''; ?></option>
  18. <option value="%" <?= isset($_GET['opt']) && $_GET['opt'] == '%' ? 'selected' : ''; ?>>%</option>
  19. </select>
  20. <input type="number" name="num2" value="<?= isset($_GET['num2']) ? $_GET['num2'] : ''; ?>">
  21. <input type="submit" value="计算">
  22. </form>
  23. </body>
  24. </html>
  25. <?php
  26. if(!empty($_GET)){
  27. $opt = $_GET['opt'];
  28. $num1 = $_GET['num1'];
  29. $num2 = $_GET['num2'];
  30. switch($opt){
  31. case '+':
  32. $num = $num1 + $num2 ;
  33. echo '计算结果:'.$num1.'+'.$num2.'='.$num;
  34. break;
  35. case '-':
  36. $num = $num1 - $num2 ;
  37. echo '计算结果:'.$num1.'-'.$num2.'='.$num;
  38. break;
  39. case '*':
  40. $num = $num1 * $num2 ;
  41. echo '计算结果:'.$num1.'×'.$num2.'='.$num;
  42. break;
  43. case '/':
  44. if($num2 == 0){
  45. echo '除数不能为0';
  46. }else{
  47. $num = $num1 / $num2 ;
  48. echo '计算结果:'.$num1.'÷'.$num2.'='.$num;
  49. }
  50. break;
  51. case '%':
  52. if($num2 == 0){
  53. echo '除数不能为0';
  54. }else{
  55. $num = $num1 % $num2 ;
  56. echo '计算结果:'.$num1.'%'.$num2.'='.$num;
  57. }
  58. break;
  59. }
  60. }
  61. ?>

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 with News 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!