Simple Calculator in PHP

In this tutorial we will see how to create a simple calculator in PHP. Our calculator will do all basic arithmetic operations like addition,subtraction, division and multiplication.

In this process I have used two input box to enter operands for mathematical operations and used one select box to choose operators.  

Calculate button is used to perform action on given operands according to selected operators. I have displayed results in an input box which have readonly attribute so that user can only see the output of calculation but will not be able to change its value.

I have created a class file having methods to perform different arithmetic operations.

Also read:

How To Get Client IP Address In PHP

10 Basic PHP String Functions

HTML

Simple calculator form is create using HTML.

<!DOCTYPE html>
<html>
<head>
<title>Simple calculator in PHP</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet">
<style>
label {
width: 140px;
text-align: left;
}​
</style>
</head>
<body>
<div class="calculator container">
<div class="col-lg-8">
<form method="post">
<div align="center">
<div class="row">
<h2>Simple Calculator in PHP</h2>
</div>
<div class="row">
<div class="col-lg-4"><label>Enter 1st Number</label> </div>
<div class="col-lg-4">
<input type="text" name="num1" class="form-control" size="20" required>
</div>
</div>

<div class="row">
<div class="col-lg-4"><label>Select Operator</label> </div>
<div class="col-lg-4"><select name="oper" class="form-control" >
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
</div>
</div>

<div class="row">
<div class="col-lg-4"><label>Enter 2nd Number</label> </div>
<div class="col-lg-4"><input type="text" name="num2" class="form-control" size="20" required>
</div>
</div>


<div class="row">
<div class="col-lg-4"><label>Results</label> </div>
<div class="col-lg-4"> <input type="text" class="form-control" readonly value="<?php echo (isset($result)) ? $result : ""; ?>">
</div>
</div>

<div class="row">
<div class="col-lg-4"><input type="submit" name="submit" class="btn btn-primary" value="Calculate">

</div>
</div>

</div> 
</form>

</div> 
</div>

</body>
</html>

PHP

PHP file is created to perform mathematical operations.

<?php
/*
* Tutorial : Simple Calculator in PHP
* Author   : Vikash Kumar Singh
* Blog     : phpcluster.com
*/


//include class file
require_once 'calc.php';

$result = "";
//create object
$cal = new calculator();

if (isset($_POST['submit'])) {

$num1 = filter_input(INPUT_POST, "num1");
$num2 = filter_input(INPUT_POST, "num2");

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

$operator = filter_input(INPUT_POST, "oper");

$result = $cal->getResult($num1, $num2, $operator);
} else {
echo "Please enter numeric value!!";
}
}
?>

View Demo

[sociallocker] Download Script [/sociallocker]

I have written this tutorial keeping in mind about beginners who wants to learn how to create simple calculator in PHP.

They can easily understand this simple scripts and they will also get concept of OOPs, methods, property, switch and how to use it.

Leave a Comment