Friday, January 22, 2016

CALCULATOR IN PHP: Create A Simple Calculator

This example is just a simple PHP script to calculate two (2) values based on the selected operator. The sample output is posted below for illustration.

Output










HTML and PHP Code
<form method="post">
<input type="number" name="first">
<select name="operator">
<option>+</option>
<option>-</option>
<option>*</option>
<option>/</option>
</select>
<input type="number" name="second">
<input type="submit" name="calc" value="Calculate">
</form>
<?php
if(isset($_POST['calc']))
{
$first=$_POST['first'];
$second=$_POST['second'];
$op=$_POST['operator'];
if($op=="+")
$answer=$first+$second;
if($op=="-")
$answer=$first-$second;
if($op=="*")
$answer=$first*$second;
if($op=="/")
$answer=$first/$second;
 
echo $answer;
}
?>

You need to used a conditional statement to be able to perform certain operation such as addition, subtraction, multiplication and division. Hope you can create your own calculator in php.

CALCULATOR IN PHP: Create A Simple Calculator

CALCULATOR IN PHP: Create A Simple Calculator Rating: 4.5 Diposkan Oleh: Emoblazz

0 comments:

Post a Comment