欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

PHP switch计算器

程序员文章站 2022-03-06 13:48:39
...
  1. <form action="1.php" method="post">
  2. <input type="number" name="num1" id="num1">
  3. <input type="number" name="num2" id="num2">
  4. <select name="fu">
  5. <option value="+">+</option>
  6. <option value="-">-</option>
  7. <option value="*">*</option>
  8. <option value="/">/</option>
  9. </select>
  10. <button>提交</button>
  11. </form>
  12. <?php
  13. if(!empty($_POST['num1'])){
  14. $num1=floatval($_POST['num1']);
  15. $num2=floatval($_POST['num2']);
  16. $fu=$_POST['fu'];
  17. }
  18. if(!empty($_POST['num1'])){
  19. switch ($fu) {
  20. case '+':
  21. echo $num1+$num2;
  22. break;
  23. case '-':
  24. echo $num1-$num2;
  25. break;
  26. case '*':
  27. echo $num1*$num2;
  28. break;
  29. case '/':
  30. echo $num1/$num2;
  31. break;
  32. }
  33. }
  34. ?>