Tuesday, December 2, 2014

How to Creating an easy calculator in PHP

Let's suppose, we would like to create an easy calculator and without using mysql. It is promising only by using “IF statement in PHP”. First you need to create a new file for writing the codes, and you can use Notepad or notepad++ for this reason, though I’ve used Notepad++ in this tutorial. Presently save the file and name it as “calculator.php”; save the file in the htdocs folder of your XAMPP Installation, because that is our root folder and we run files from that folder on our local server (Local host). Now for creating a calculator you need to first create a form in HTML and add some input tags. Here is the HTML code for the calculator, and we'll later on add PHP code to this:

f(isset($_POST['submit'])){
$value1 = $_POST['value1'];
$value2 = $_POST['value2'];
$action = $_POST['action'];
if($action=="+"){
echo "Your Answer is:
";
echo $value1+$value2;
}
if($action=="-"){
echo "Your Answer is:
";
echo $value1-$value2;
}
if($action=="*"){
echo "Your Answer is:
";
echo $value1*$value2;
}
if($action=="/"){
echo "Your Answer is:
";
echo $value1/$value2;
}
}
?>

Now check your online calculator in your browser, and retain information always open your PHP files using this address: localhost. Every now and then wrongly you directly click on the file which is inside a folder.


I hope you have enjoyed and learnt something new from this tutorial.