Solution: Simple PHP Calculator with source code, demo, and also how you can execute full program easily. To complete the program you need to follow those steps given bellow
Live Demo
Also Helpful –
PHP Age Calculator with Source Code
PHP Calculator with Source Code
Hi
Back to coding, To complete this program with debugging free code you have to create only one file (index.php) because we use the bootstrap framework to style our CSS. And we also post data on the same page using the PHP function “isset($_POST[‘submit’]))”
that only shows data ones it’s submitted.
index.php
Create a PHP file named ‘index.php‘ and put those codes given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
<!-- Coding Debugging (https://codingdebugging.com) --> <!DOCTYPE html> <html lang="en"> <head> <title>PHP Calculator - Coding Debugging</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- Bootstrap CSS --> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css"> <!-- Bootstrap JS --> <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script> <!-- Font Awesome --> <script src="https://kit.fontawesome.com/996973c893.js" crossorigin="anonymous"></script> <!-- Google Fonts --> <link href="https://fonts.googleapis.com/css2?family=Baloo+Thambi+2:wght@400;500;600;700;800&display=swap" rel="stylesheet"> <!-- My Stylesheet --> <style type="text/css"> body { font-family: "Baloo Thambi 2", cursive; } </style> </head> <body> <div class="container-fluid bg-light p-5 text-center my-3"> <h2 class="">PHP Calculator</h1> <h5 class="text-muted">SIMPLE PHP CALCULATOR</h5> </div> <div class="container bg-light p-3 my-5"> <div class="card-body"> <form action="index.php" method="post" class="form-group"> <div class="row mb-3"> <div class="col-md-4"> <input name="number1" type="number" class="form-control" placeholder="Number 1" value="<?php echo $_POST['number1'];?>"/> </div> <div class="col-md-4"> <select name="operation" class="form-control"> <option value="plus">Plus (+)</option> <option value="minus">Minus (-)</option> <option value="times">Times (*)</option> <option value="divided">Divided (/)</option> </select> </div> <div class="col-md-4"> <input name="number2" type="number" class="form-control" placeholder="Number 2" value="<?php echo $_POST['number2'];?>"/> </div> </div> <div class="row text-right"> <div class="col-md-12"> <input type="submit" name="submit" class="btn btn-primary " value="Calculate"> </div> </div> </form> </div> <div class="card-footer text-center"> <?php // If the submit button has been pressed if(isset($_POST['submit'])) { // Check number values if(is_numeric($_POST['number1']) && is_numeric($_POST['number2'])) { // Calculate total if($_POST['operation'] == 'plus') { $total = $_POST['number1'] + $_POST['number2']; } if($_POST['operation'] == 'minus') { $total = $_POST['number1'] - $_POST['number2']; } if($_POST['operation'] == 'times') { $total = $_POST['number1'] * $_POST['number2']; } if($_POST['operation'] == 'divided') { $total = $_POST['number1'] / $_POST['number2']; } // Print total to the browser echo "<h1>{$_POST['number1']} {$_POST['operation']} {$_POST['number2']} equals {$total}</h1>"; } else { // If numbers are invaild echo '<h2>Numeric values are required!!!</h2>'; } } ?> </div> </div> <hr> <footer class="footer mt-auto py-3 bg-light"> <div class="container text-center"> <span class="text-muted">Copyright ©2020 | PHP Calculator | <a href="https://codingdebugging.com/" target="_blank">CodingDebugging.Com</a></span> </div> </footer> </body> </html> |
Thank you for visiting!
We appreciate you for your precious time. I hope you are learn something from here! If you have any queries about this coding comment down below we will reply ASAP.
Keep coding, Keep debugging!