Skip to main content
Key2Goal

Main navigation

  • Home
  • About Us
  • Contact us
  • Glossary
  • Privacy Policy
User account menu
  • Log in

Breadcrumb

  1. Home

Calculator program HTML & PHP

Simple calculator program in PHP that performs basic arithmetic operations:

<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $num1 = $_POST['num1'];
    $num2 = $_POST['num2'];
    $operator = $_POST['operator'];

    $result = '';

    switch ($operator) {
        case '+':
            $result = $num1 + $num2;
            break;
        case '-':
            $result = $num1 - $num2;
            break;
        case '*':
            $result = $num1 * $num2;
            break;
        case '/':
            if ($num2 != 0) {
                $result = $num1 / $num2;
            } else {
                $result = 'Cannot divide by zero';
            }
            break;
        default:
            $result = 'Invalid operator';
    }
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Calculator</title>
</head>
<body>
    <h2>Calculator</h2>
    <form method="POST" action="">
        <input type="number" name="num1" required>
        <select name="operator">
            <option value="+">+</option>
            <option value="-">-</option>
            <option value="*">*</option>
            <option value="/">/</option>
        </select>
        <input type="number" name="num2" required>
        <input type="submit" value="Calculate">
    </form>

    <?php if (isset($result)) : ?>
        <h4>Result: <?php echo $result; ?></h4>
    <?php endif; ?>
</body>
</html>

This PHP script displays a simple HTML form where you can enter two numbers and select an operator. After submitting the form, the script retrieves the input values, performs the corresponding arithmetic operation based on the selected operator, and displays the result below the form.

Note: its just a basic example can't be use on production prupose.

  • Log in to post comments

Comments

Article category

  • Drupal
  • Wordpress
  • Java
  • Javascipt
  • HTML
  • CSS
  • IOS
  • Android
  • PHP
  • MySQL
  • Other
  • Command line
  • NodeJs
  • Drupal 8
  • Drupal 7
  • GIT
  • DevOps
  • Python
  • Xampp
  • Server
  • Artificial intelligence
  • AppScript
  • Windows

Recent content

  • ERROR: Cannot create symbolic link : A required privilege is not held by the client.
    3 hours 33 minutes ago
  • Differences Between Bootstrap 3 & Bootstrap 4
    1 month ago
  • About Us
    1 month ago
  • Privacy Policy
    1 month ago
  • Create a Virtual Host on Xampp in 5 steps
    3 months ago
  • How to setup Multisite in drupal 9
    9 months ago
  • How to creating a FOR loop and a WHILE loop in JavaScript
    9 months ago
  • Calculator program HTML & PHP
    9 months ago
  • Algothrim to create a web application
    9 months ago
  • 10 Git Useful Commands for Every Developer
    9 months ago
© Copyright 2017 - 2026 key2Goal. All Rights Reserved