Simple Calculator Using PHP – Calculator Example With Source Code

calculator using PHP program to create simple calculator for addition, subtraction, multiplication and division.

Simple Calculator Using PHP

How to Create a calculator in PHP? – PHP calculator code using switch case & Else-If statement. This is a simple calculator using PHP that can perform the simple arithmetic operations (+, -, * and /).


  • Addition
  • Subtraction
  • Multiplication
  • Division

HTML Code for Simple Calculator

<h2>PHP Program to Make a Simple Calculator</h2><hr>
First Number:<input name="first_number" value=""><br>
Second Number:<input name="second_number" value=""><br>
<input type="submit" name="calculation" value="+">
<input type="submit" name="calculation" value="-">
<input type="submit" name="calculation" value="x">
<input type="submit" name="calculation" value="/"><br>
<br>Result: <input type='text' value="<?php echo $ans; ?>"><br>

PHP Code for Simple Calculator

I am using simple PHP Else-If statement for performing the functions.

	else if($results=="-")
	else if($results=="x")
	else if($results=="/")

Don’t Miss : Mifflin St Jeor formula

Write a simple calculator program in PHP using switch case

using switch case

Full Source code Example for calculator using php

<!DOCTYPE html>

	<title>Simple Calculator Program in PHP - Tutorials Class</title>

$first_number = $_POST['first_number'];
$second_number = $_POST['second_number'];
$operator = $_POST['operator'];
$result = '';
if (is_numeric($first_number) && is_numeric($second_number)) {
    switch ($operator) {
        case "Add":
           $result = $first_number + $second_number;
        case "Subtract":
           $result = $first_number - $second_number;
        case "Multiply":
            $result = $first_number * $second_number;
        case "Divide":
            $result = $first_number / $second_number;


    <div id="calc-cart">
	<h2>PHP - Simple Calculator Program</h2>
	  <form action="" method="post" id="calc-form">
                <input type="number" name="first_number" id="first_number" required="required" value="<?php echo $first_number; ?>" /> <b>First Number</b>
                <input type="number" name="second_number" id="second_number" required="required" value="<?php echo $second_number; ?>" /> <b>Second Number</b>
                <input readonly="readonly" name="result" value="<?php echo $result; ?>"> <b>Result</b>
            <input type="submit" name="operator" value="Sum" />
            <input type="submit" name="operator" value="Subtract" />
            <input type="submit" name="operator" value="Multiply" />
            <input type="submit" name="operator" value="Divide" />

I hope you get an idea about calculator using php.
I would like to have feedback on my
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.