Skip to content

Pratama Digital

Your Digital Partner

Menu
  • Home
  • Download Area
  • Pratama Digital Net
  • Internet Speed Test
  • About Us
Menu

Creating a Simple Calculator Script with PHP

Posted on September 20, 2023

Introduction

PHP is a versatile scripting language often used for web development, but it can also be employed to create command-line applications. In this tutorial, we will guide you through the process of building a simple calculator script using PHP. This script will perform basic arithmetic operations such as addition, subtraction, multiplication, and division.

Prerequisites

Before we get started, make sure you have PHP installed on your computer. You can download PHP from the official website (https://www.php.net/downloads.php) or use a pre-installed package like XAMPP or WAMP.

Building the Calculator Script

  1. Create a New PHP FileOpen a text editor or an integrated development environment (IDE) and create a new PHP file. You can name it calculator.php.
  2. Write the PHP CodeAdd the following PHP code to your calculator.php file:

    <?php
    // Check if the form has been submitted
    if (isset($_POST[‘submit’])) {
    // Get the user’s input
    $num1 = $_POST[‘num1’];
    $num2 = $_POST[‘num2’];
    $operator = $_POST[‘operator’];

    // Perform the calculation based on the selected operator
    switch ($operator) {
    case ‘add’:
    $result = $num1 + $num2;
    break;
    case ‘subtract’:
    $result = $num1 – $num2;
    break;
    case ‘multiply’:
    $result = $num1 * $num2;
    break;
    case ‘divide’:
    if ($num2 != 0) {
    $result = $num1 / $num2;
    } else {
    $result = ‘Division by zero is not allowed’;
    }
    break;
    default:
    $result = ‘Invalid operator’;
    }
    }
    ?>

    <!DOCTYPE html>
    <html>
    <head>
    <title>Simple Calculator</title>
    </head>
    <body>
    <h1>Simple Calculator</h1>
    <form method=”post” action=”calculator.php”>
    <input type=”number” name=”num1″ placeholder=”Enter first number” required>
    <select name=”operator”>
    <option value=”add”>+</option>
    <option value=”subtract”>-</option>
    <option value=”multiply”>*</option>
    <option value=”divide”>/</option>
    </select>
    <input type=”number” name=”num2″ placeholder=”Enter second number” required>
    <input type=”submit” name=”submit” value=”Calculate”>
    </form>
    <?php
    // Display the result if available
    if (isset($result)) {
    echo ‘<h2>Result: ‘ . $result . ‘</h2>’;
    }
    ?>
    </body>
    </html>

     

    This PHP script takes user input for two numbers and an operator (addition, subtraction, multiplication, or division). It then calculates and displays the result.

  3. Testing the CalculatorSave the calculator.php file and open it in a web browser. You should see a simple calculator form. Enter values and select an operator, then click the “Calculate” button to see the result displayed on the page.

Conclusion

You’ve successfully created a basic calculator script using PHP. This script can perform common arithmetic operations and display the results to the user. You can further enhance this script by adding more features, such as handling decimal numbers or adding error handling for edge cases. PHP’s versatility makes it a great choice for building simple web-based applications like this calculator.

Share this:

  • Facebook
  • X

Related

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • 10 Powerful Alternatives to MikroTik for Small to Enterprise Networks
  • LDAP: A Guide for Linux System Administrators
  • Virtual Private Server (VPS): A Guide for Beginners
  • Forward Ports on Your Router for Online Games
  • MikroTik RouterOS 6 vs. RouterOS 7: Key Differences, Advantages, and Disadvantages

Tags

24h clock adobe adobe cc adobe cs master almalinux arduino autodesk basic ip address cad software calculator php connect java dns server download photoshop ecmp formula ipv4 innodb install iptables ipsec iptables java mysql linux firewall linux mint linux security load balance microsoft office myisam mysql mysql engine node.js nodejs nth office alternative openvpn pcc php port game online port mikrotik postgresql postgresql to mysql public dns rocky linux running text script php ubuntu LTS wireguard

Categories

  • Advertising
  • CAD software
  • Computer Component
  • Control Panel
  • Database
  • DNS
  • Download
  • File Server
  • Firewall
  • Graphic Design
  • Internet
  • IP Address
  • Linux OS
  • Load Balance
  • Mikrotik
  • Movie
  • Novel/Book
  • Office
  • OpenWRT
  • Processor & GPU
  • Programming
  • Routing
  • Security
  • Server
  • Tutorial
  • Video Editing
  • VPN
  • Web Programming
  • Windows OS
  • Wordpress

About Us

We are a company specializing in network configuration, MikroTik and computer installation services, as well as wifi setup. With experience dating back to 2017, we have gained the trust of numerous clients from various regions who rely on our services. Our expertise extends to catering to personal, office, institutional, and industrial needs.

Archives

  • December 2024
  • September 2024
  • June 2024
  • May 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • September 2023

Recent Posts

  • 10 Powerful Alternatives to MikroTik for Small to Enterprise Networks
  • LDAP: A Guide for Linux System Administrators
  • Virtual Private Server (VPS): A Guide for Beginners
  • Forward Ports on Your Router for Online Games
  • MikroTik RouterOS 6 vs. RouterOS 7: Key Differences, Advantages, and Disadvantages
©2025 Pratama Digital | Design: Newspaperly WordPress Theme