Thursday, March 22, 2012

How to Make Simple Calculator in Php

 Here I am writing php code using drop down menu to select basic math operations. It is good for beginners in PHP Programming. If you like it or have some questions about it please leave a comment thanks!



<?php
error_reporting(0);
if(isset($_POST['clear'])){
    $input1 = '';
    $input2 = '';
    }

if(isset($_POST['submit'])){
   
    $input1 = $_POST['input1'];   
    $input2 = $_POST['input2'];
    $operator = $_POST['operator'];
   
if($operator == 'add'){
        $result = $input1 + $input2;
        echo "The Result is: ".$result;
        }
else if($operator == 'divide'){
        $result = $input1 / $input2;
        echo "The Result is: ".$result;
        }
else if($operator == 'multi'){
        $result = $input1 * $input2;
        echo "The Result is: ".$result;
        }
else if($operator == 'sub'){
        $result = $input1 - $input2;
        echo "The Result is: ".$result;
        }
else    {echo "Error";}

    }

?>

<form method="post" action="">

<input type="text" name="input1" size="10" value="<?php echo $input1;?>" />

<select name="operator" >
<option value="add">+</option>
<option value="divide">/</option>
<option value="multi">x</option>
<option value="sub">-</option>
</select>

<input type="text" name="input2" size="10" value="<?php echo $input2;?>" />

<input type="submit" name="submit" value="Submit" />
<input type="submit" name="clear" value="Clear" />

</form>

No comments:

Post a Comment