Building a Basic Calculator in PHP


PHP (Hypertext Preprocessor) is a versatile scripting language that is particularly well-suited for web development. In this article, we'll guide you through creating a basic calculator using PHP, showcasing its ability to handle basic arithmetic operations and demonstrating how PHP can interact with HTML forms.

Setting Up the HTML Form

First, let's create an HTML form where users can input two numbers and select an arithmetic operation. This form will use the POST method to send data to a PHP script for processing.

<!DOCTYPE html>



    <title>Basic Calculator</title>



    <h1>PHP Calculator</h1>

    <form method="post">

        <input type="number" name="num1" placeholder="Enter first number" required>

        <input type="number" name="num2" placeholder="Enter second number" required>

        <select name="operation" required>

            <option value="add">Addition</option>

            <option value="subtract">Subtraction</option>

            <option value="multiply">Multiplication</option>

            <option value="divide">Division</option>


        <button type="submit">Calculate</button>



    if ($_SERVER["REQUEST_METHOD"] == "POST") {

        $num1 = $_POST['num1'];

        $num2 = $_POST['num2'];

        $operation = $_POST['operation'];


        if (is_numeric($num1) && is_numeric($num2)) {

            switch ($operation) {

                case 'add':

                    $result = $num1 + $num2;

                    echo "<p>The result of addition is: $result</p>";


                case 'subtract':

                    $result = $num1 - $num2;

                    echo "<p>The result of subtraction is: $result</p>";


                case 'multiply':

                    $result = $num1 * $num2;

                    echo "<p>The result of multiplication is: $result</p>";


                case 'divide':

                    if ($num2 != 0) {

                        $result = $num1 / $num2;

                        echo "<p>The result of division is: $result</p>";

                    } else {

                        echo "<p>Division by zero is not allowed.</p>";




                    echo "<p>Invalid operation selected.</p>";



        } else {

            echo "<p>Please enter valid numbers.</p>";






Breaking Down the Code

  1. HTML Form Structure

    • The form includes two input fields for numbers and a dropdown menu for selecting the arithmetic operation.
    • The method="post" attribute specifies that the form data should be sent to the server using the POST method when the user submits the form.
  2. Handling the Form Submission in PHP

    • PHP code within the same file processes the form data after submission. The $_SERVER["REQUEST_METHOD"] variable checks if the form has been submitted using the POST method.
    • $_POST['num1'], $_POST['num2'], and $_POST['operation'] retrieve the user inputs.
  3. Validating and Processing the Input

    • The is_numeric() function ensures that the input values are numbers.
    • A switch statement performs the selected arithmetic operation. For each case (addition, subtraction, multiplication, division), the corresponding arithmetic operation is executed, and the result is displayed.
    • Division includes an additional check to prevent division by zero.


Creating a basic calculator in PHP is an excellent way to understand the interaction between HTML forms and PHP scripts. This example demonstrates essential concepts such as form handling, user input validation, and basic arithmetic operations in PHP. With this foundational knowledge, you can expand the calculator's functionality to include more complex operations, user interface improvements, and error handling mechanisms, further enhancing your PHP development skills.