How to create a calculator in PHP

By | May 19, 2019

I have pasted code below that how to create tutorial in php.

Soon I will post video tutorial on my YouTube channel or on my site.

First of all to learn and create calculator by yourself first copy below code and save as index.php.

Istall XAMP

Go to htdocs folder

and paste your file index.php

start your xamp controll panel

then go to your browser and type local host

you will see many files which are in your htdocs folder

type in your browser like this

//locallhost/index.php

or when you type locallhost in your browser. Then your browser will show you list of files which are in your htdocs folder.

calculator in php
calculator in php
<!DOCTYPE Html>
<html>
<head>
<title></title>
</head>
<body>

<form>
	<input type = "text" name = "num1" placeholder = "Number 1" ><br>
	<input type = "text" name = "num2" placeholder = "Number 2" ><br>

	<select name= "operator">
		<option>NONE</option>
		<option>ADD</option>
		<option>SUBTRACTION</option>
		<option>Multiplication</option>
		<option>Modulus</option>
		<option>Division</option>
		<option>Power</option>
	</select>
	
	<button  type="submit"name="submit" value="submit">Calculate</button><br>
</form>	
<p>The answer is</p>

<?php

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

$number1 = $_GET['num1'];
$number2 = $_GET['num2'];
$opr = $_GET['operator'];

switch($opr){

case 'NONE':
     echo "Please select your operator";
     break;

case 'ADD':
     echo $number1 + $number2;
     break;

case 'SUBTRACTION':
     echo $number1-$number2;
     break;

case'Multiplication':
     echo $number1*$number2;
     break;

case 'Modulus':
     echo $number1% $number2;
     break;

case 'Division':
     echo $number1/$number2;
     break;

case 'Power':
   echo $number1**$number2;

default:
   echo "Please Try again";


}

}





?>

</body>
</html>

Leave a Reply

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