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 /).

Operations:

  • 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>
</form>

PHP Code for Simple Calculator

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

<?php
if(isset($_POST['calculation'])){
	$nomber1=$_POST['first_number'];
	$nomber2=$_POST['second_number'];
	$results=$_POST['calculation'];
	if($results=="+")
	$ans=$nomber1+$nomber2;
	else if($results=="-")
	$ans=$nomber1-$nomber2;
	else if($results=="x")
	$ans=$nomber1*$nomber2;
	else if($results=="/")
	$ans=$nomber1/$nomber2;
}
?>

Don’t Miss : Mifflin St Jeor formula

Read Also:  PHP MySQL ajax Autocomplete Live Search Box in jQuery

Write a simple calculator program in PHP using switch case

using switch case

Full Source code Example for calculator using php

<!DOCTYPE html>

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

<?php
$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;
            break;
        case "Subtract":
           $result = $first_number - $second_number;
            break;
        case "Multiply":
            $result = $first_number * $second_number;
            break;
        case "Divide":
            $result = $first_number / $second_number;
    }
}

?>

<body>
    <div id="calc-cart">
	<h2>PHP - Simple Calculator Program</h2>
	  <form action="" method="post" id="calc-form">
            <p>
                <input type="number" name="first_number" id="first_number" required="required" value="<?php echo $first_number; ?>" /> <b>First Number</b>
            </p>
            <p>
                <input type="number" name="second_number" id="second_number" required="required" value="<?php echo $second_number; ?>" /> <b>Second Number</b>
            </p>
            <p>
                <input readonly="readonly" name="result" value="<?php echo $result; ?>"> <b>Result</b>
            </p>
            <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" />
	  </form>
    </div>
</body>
</html>

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