Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $operations = ['+', '-', '*', '/'];
- if (isset($_POST['op1']) && isset($_POST['op2'])) {
- if (in_array($_POST['op'], $operations)) {
- $op1 = (float)$_POST['op1'];
- $op2 = (float)$_POST['op2'];
- switch ($_POST['op']){
- case '+':
- $res = $op1 + $op2;
- break;
- case '-':
- $res = $op1 - $op2;
- break;
- /*case '*':
- $res = $op1 * $op2;
- break;
- case '/':
- $res = $op1 / $op2;
- break;*/
- }
- } else {
- $res = 'error!';
- }
- }
- ?>
- <html>
- <head>
- </head>
- <body>
- <form action="calculator.php" method="post">
- <input type="text" name="op1" value="<?php echo $op1; ?>"/>
- <select name="op">
- <?php foreach ($operations as $op): ?>
- <option value="<?php echo $op; ?>"><?php echo $op; ?></option>
- <?php endforeach; ?>
- </select>
- <input type="text" name="op2" value="<?php echo $op2; ?>"/>
- <br>
- <?php if(isset($res)): ?>
- <p>Result: <?php echo $res; ?></p>
- <?php endif; ?>
- <input type="submit" value="calc!" />
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement