calculator.php 806 B

1234567891011121314151617181920212223242526272829303132
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Calculator</title>
  5. </head>
  6. <body>
  7. <h1>PHP Math Calculator</h1>
  8. <p>Input any math expression (parenthesis included)</p>
  9. <form action="" method="GET">
  10. <input type="text" name="math_exp" id="math_exp" size="50">
  11. <input type="submit" value="submit">
  12. </form>
  13. <h2>Solution:</h2>
  14. <?php
  15. require __DIR__ . '/vendor/autoload.php';
  16. use Parse\MathParser;
  17. use Parse\ParseException;
  18. use Exception;
  19. try {
  20. $exp = MathParser::parse($_GET['math_exp']);
  21. eval("\$solution = {$exp};");
  22. echo "{$exp} = {$solution}";
  23. } catch (ParseException $e1) {
  24. echo "Invalid Expression!";
  25. } catch (Exception $e2) {
  26. echo "Some other error occured!";
  27. }?>
  28. </body>
  29. </html>