Blogger Information
Blog 21
fans 0
comment 0
visits 14825
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
(1119)使用条件分支结构与 php 运算符模拟实现基本的简单的计算器功能~
Yuming
Original
674 people have browsed it

(1119)使用条件分支结构与 php 运算符模拟实现基本的简单的计算器功能~

  1. <form action="" method='post'>
  2. <input type="text" name='bef' value="<?php echo $_POST['bef']; ?>">
  3. <select name='opt'>
  4. <option value ="+" selected='<?php echo $_POST['opt']=="+" ? "selected": "" ?>' >+</option>
  5. <option value ="-" selected='<?php echo $_POST['opt']=="-" ? "selected": "" ?>' >-</option>
  6. <option value="*" selected='<?php echo $_POST['opt']=="*" ? "selected": "" ?>' >*</option>
  7. <option value="/" selected='<?php echo $_POST['opt']=="/" ? "selected": "" ?>' >/</option>
  8. </select>
  9. <input type="text" name='aft' value="<?php echo $_POST['aft']; ?>">
  10. <input type='submit' name='calc'></input>
  11. </form>
  12. <?php
  13. // var_dump( $_POST['bef'],$_POST['aft'],$_POST['opt']);
  14. $res;
  15. if($_POST['bef'] =='' || $_POST['aft']=='')
  16. {
  17. echo '请填写完整得表达式!';
  18. return;
  19. };
  20. switch ($_POST['opt']) {
  21. case '+':
  22. $res = $_POST['bef'] + $_POST['aft'];
  23. break;
  24. case '-':
  25. $res = $_POST['bef'] - $_POST['aft'];
  26. break;
  27. case '*':
  28. $res = $_POST['bef'] * $_POST['aft'];
  29. break;
  30. case '/':
  31. $res = $_POST['bef'] / $_POST['aft'];
  32. break;
  33. }
  34. echo '结果为:' . $res ;
  35. ?>

总结:第一次体验到php模板语法的魅力,嵌入到 HTML里面有点react的味道,但是相对于vue的模板语法和react的JSX确实比较难写,不过能体会到php的强大之处就是能嵌入到HTML,也是不错的

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
1 comments
灭绝师太 2020-11-23 13:57:13
可以将input的元素属性type设置为number,可以有效过滤用户的输入~
1 floor
Author's latest blog post