Simple Calculator Using PHP
Simple Calculator Using PHP
This is a simple calculator that can perform the simple arithmetic tasks (+, -, * and /).
Write a simple calculator program in PHP using if-else statement
HTML Code for Simple Calculator
1 2 3 4 5 6 7 8 9 |
<h1>Simple Calculator</h1><br> First Number:<input name="n1" value=""><br> Second Number:<input name="n2" value=""><br> <input type="submit" name="sub" value="+"> <input type="submit" name="sub" value="-"> <input type="submit" name="sub" value="x"> <input type="submit" name="sub" value="/"><br> <br>Result: <input type='text' value="<?php echo $ans; ?>"><br> </form> |
PHP Code for Simple Calculator
We are using Else-If statement for performing the functions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php if(isset($_POST['sub'])){ $num1=$_POST['n1']; $num2=$_POST['n2']; $oprnd=$_POST['sub']; if($oprnd=="+") $ans=$num1+$num2; else if($oprnd=="-") $ans=$num1-$num2; else if($oprnd=="x") $ans=$num1*$num2; else if($oprnd=="/") $ans=$num1/$num2; }?> |
Download Code – Simplecalculator
Write a simple calculator program in PHP using the switch statement
Operations on calculator
Following operations will perform on the given calculator;
- Multiplication
- Division
- sum
- Subtraction
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
<!DOCTYPE html> <head> <title> Write a simple calculator program in PHP using the switch statement by T4Tutorials.com </title> </head> <?php $FirstNumber = $_POST['FirstNumber']; $SecondNumber = $_POST['SecondNumber']; $operator = $_POST['operator']; $CalculatorResult = ''; if (is_numeric($FirstNumber) && is_numeric($SecondNumber)) { switch ($operator) { case "Sum": $CalculatorResult = $FirstNumber + $SecondNumber; break; case "Subtraction": $CalculatorResult = $FirstNumber - $SecondNumber; break; case "Multiplication": $CalculatorResult = $FirstNumber * $SecondNumber; break; case "Division": $CalculatorResult = $FirstNumber / $SecondNumber; } } ?> <body> <div id="page-wrap"> <h1>PHP - Simple Calculator Program</h1> <form action="" method="post" id="quiz-form"> <p> <input type="number" name="FirstNumber" id="FirstNumber" required="required" value="<?php echo $FirstNumber; ?>" /> <b>First Number</b> </p> <p> <input type="number" name="SecondNumber" id="SecondNumber" required="required" value="<?php echo $SecondNumber; ?>"
/> <b>Second Number</b> </p> <p> <input readonly="readonly" name="CalculatorResult" value="<?php echo $CalculatorResult; ?>"> <b>CalculatorResult</b> </p> <input type="submit" name="operator" value="Sum" /> <input type="submit" name="operator" value="Subtraction" /> <input type="submit" name="operator" value="Multiplication" /> <input type="submit" name="operator" value="Division" /> </form> </div> </body> </html> |
Calculator in PHP using function
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<?php $FirstNumber = $_POST['FirstNumber']; $SecondNumber = $_POST['SecondNumber']; $Result = $_POST['Result']; function MyCalculator($Number1,$Number2, $Result) // set $Result as parameter { switch($Result) { case "Addition": // here you have to use colons not semi-colons $compute = $Number1 + $Number2; break; case "Subtraction": $compute = $Number1 - $Number2; break; case "Multiplication": $compute = $Number1 * $Number2; break; case "Division": $compute = $Number1 / $Number2; break; } return $compute; // returning variable } echo "$Result <br /> <br /> 1st Number: $FirstNumber <br /> 2nd Number: $SecondNumber <br /><br />"; echo "Answer is:" .MyCalculator($FirstNumber,$SecondNumber, $Result); // you need to pass $Result as argument of that function ?> |
PHP calculator using class
Write a PHP program to make a calculator class that will accept two values as a parameter, then perform the operation of addition, multiplication, subtraction, and division.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<?php class MyCalculator { private $FirstNumber, $SecondNumber; public function __construct( $Value1, $Value2 ) { $this->FirstNumber = $Value1; $this->SecondNumber = $Value2; } public function Addition() { return $this->FirstNumber + $this->SecondNumber; } public function Subtraction() { return $this->FirstNumber - $this->SecondNumber; } public function Multiplication() { return $this->FirstNumber * $this->SecondNumber; } public function Division() { return $this->FirstNumber / $this->SecondNumber; } } $mycalc = new MyCalculator(6, 3); echo $mycalc-> Addition()."\n"; // Displays 9 echo $mycalc-> Multiplication()."\n"; // Displays 18 echo $mycalc-> Subtraction()."\n"; // Displays 3 echo $mycalc-> Division()."\n"; // Displays 2 ?> |
More Examples of Calculators
- PHP Calculator using GET method
- PHP mortgage calculator script
- Age calculator in PHP
- GPA calculator in PHP
- Scientific calculator in PHP with database
- Interest, Saving or Profit Calculator in PHP
- PHP grade calculator code
- PHP Calculator using Switch statement
- PHP Calculator with Class and Objects
- Calculator design in CSS and Javascript
Topic Covered:
Calculator in PHP using the button, PHP calculator code, how to make a calculator in PHP, PHP code for a calculator with buttons, PHP calculator form, calculator in PHP and HTML, PHP calculator script, create a calculator in PHP, PHP calculator source code download.