Blogger Information
Blog 14
fans 0
comment 0
visits 9492
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP 运算符与简易计算器
Mr.Ran
Original
486 people have browsed it

运算符

  • 算术运算符
    +、减 -、乘 \*、除 /、取余 %
  • 赋值运算符
    =、加 +=、减 -=、乘 \*=、除 /=、取余 %=
  • 递增/递减运算符
    先递增 ++$a、后递增 $a++、先递减 --$a 、后递减 $a--
  • 比较运算符
    等于 ==、不等于 !=、大于 > 、小于 <、大于等于 >=、小于等于 <= 、恒等于 ===、恒不等于 !==
  • 逻辑运算符
    &&、或 ||、非 !、异或 xor

简易计算器

PHP 代码部分:

  1. $n1 = (int)isset($_POST["num1"])?$_POST["num1"]:"";
  2. $n2 = (int)isset($_POST["num2"])?$_POST["num2"]:"";
  3. $opt = isset($_POST["opt"])?$_POST["opt"]:"1";
  4. $v = null;
  5. if (!empty($_POST)) {
  6. switch ($opt) {
  7. case '1':
  8. $v = $n1 + $n2;
  9. break;
  10. case '2':
  11. $v = $n1 - $n2;
  12. break;
  13. case '3':
  14. $v = $n1 * $n2;
  15. break;
  16. case '4':
  17. $v = $n1 / $n2;
  18. break;
  19. case '5':
  20. $v = $n1 % $n2;
  21. break;
  22. default:
  23. $v = 0;
  24. break;
  25. }
  26. }

HTML 代码部分:

  1. <!DOCTYPE html>
  2. <html lang="en">
  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. <style>input,select{max-width:50px; height:20px; line-height:20px;}</style>
  9. </head>
  10. <body>
  11. <h4>简易计算器</h4>
  12. <form action="calc.php" method="post">
  13. <input type="text" name="num1" value="<?=$n1?>">
  14. <select name="opt">
  15. <option value="1" <?=$opt=="1"?'selected':""?> >+</option>
  16. <option value="2" <?=$opt=="2"?'selected':""?> >-</option>
  17. <option value="3" <?=$opt=="3"?'selected':""?> >*</option>
  18. <option value="4" <?=$opt=="4"?'selected':""?> >/</option>
  19. <option value="5" <?=$opt=="5"?'selected':""?> >%</option>
  20. </select>
  21. <input type="text" name="num2" value="<?=$n2?>">
  22. <input type="submit" value="=">
  23. <input type="text" value="<?=$v?>">
  24. </form>
  25. </body>
  26. </html>

效果展示:

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