Make Simple Calculator in PHP – With Validation


@ UNLOCK YOUR IMAGINATION


Project
                                                                                               Why PHP Is The Best Programming 
Make Simple Calculator in PHP – With Validation

If you have Learn something about HTML,CSS and PHP then you should try to make some project with this languages. and in this article we also going to learn how to make a Calculator in PHP.It Looks like this.

This is Our Calculator that we learn to make.
In this Calculator you are able to Add , Subtract ,Multiply or Divide Data . So Below I give  the source Code of this Calculator.You just copy this code and Paste it in a text editor like notepad,Notepad++ and save file in PHP. and Run It.

 Source code to making Calculator in PHP .

<!–Copy this Code for making Calculator in PHP –>
<html>
<head>
<title>
Calculator [SawanWap.com] </title>
</head>
<body bgcolor=”#ccc”>
<form method=”get”>
<table width=”500″ border=”1px” bgcolor=”#eee” cellpadding=”5px”> <!–Table Start From you can change border or background–>
<th colspan=”2″ bgcolor=”pink”> <font size=”5″> PHP Calculator</font> </th>
<tr align=”center” id=”Value”>
<td>First Value: </td><td> <input type=”text” name=”f_value” id=”value_field” /> </td>
</tr>
<tr align=”center” id=”Value”>
<td>Second Value: </td>
<td> <input type=”text” name=”s_value” id=”value_field” /> </td>
</tr>
<tr align=”center” id=”Value”>
<td >
Select Operation:
</td>
<td >
<select name=”operation” id=”operation”>
<option value=””> Select Option</option>
<option value=”+”>Add (+) </option>
<option value=”-“>Sub (-) </option>
<option value=”*”>Mul (*) </option>
<option value=”/”>Div (/) </option>
</select>
</td>
</tr>
<tr><td colspan=”2″ align=”center”><Input Type=”submit” name=”submit” value=”Calculate” id=”calculator”/> </td></tr>
<tr align=”center”> <td id=”Value”>Your Result:-</td><td id=”result”><?php cal(); ?></td> </tr>
</table>
</form>
</body>
</html>
<?php
function cal(){
if(isset($_GET[‘submit’])){
$f_value=$_GET[‘f_value’];
$s_value=$_GET[‘s_value’];
$opration=$_GET[‘operation’];
if (is_numeric($f_value)) {
if (is_numeric($s_value)) {
if($opration==””){
echo “<script>alert(‘Choose Operation’); </script>”;
exit();
}
else if ($opration==”+”){
$adding= $f_value +$s_value;
echo “<b>Add</b> “.$f_value.”+”.$s_value.”= <b>”. $adding.”</b>”;
exit();
}
else if ($opration==”-“){
$sub= $f_value -$s_value;
echo “<b>Subtract</b> “.$f_value.”-“.$s_value.”= <b> “. $sub.”</b>”;
exit();
}
else if ($opration==”*”){
$mul= $f_value*$s_value;
echo “<b>Multiply </b>”.$f_value.”*”.$s_value.”= <b>”. $mul.”</b>”;
exit();
}
else if ($opration==”/”){
$div= $f_value /$s_value;
echo “Divide “.$f_value.”/”.$s_value.”= “. $div;
exit();
}
}
else{
echo “<script> alert(‘Enter SECOND Numerical Value’);</script>” ;
}}
else{
echo “<script> alert(‘Enter First Numerical Value’);</script>” ;
}}
//here is submit condition else
else{
echo “Submit Your Detail”;
}
}
?>
Note:- I Use some PHP validation to add some Extra function in calculator.
In the Above Code , I create a Table and In this table create a Form that take input from User.and at the bottom of source code I use PHP to Calculating data.One More thing I have not Use any CSS Code For Styling Form.So it Not look good ,If you want to style this form then Copy and paste below code in </head> section of page.
<!–styling code is here Paste it in head section–>
<style rel=”stylesheet” type=”text/css”>
#value_field{
height:30px;
border-radius:5px;
}
#value{
font-size:20px;
font-weight:bold;
}
#operation{
height:30px;
border-radius:5px;
}
#calculator{
height:30px;
width:85px;
background:#A4A4A4;
color:white;
border:1px solid #ccc;
border-bottom:3px solid #5B5B5B;
}
#calculator:hover{
background:#B2B2B2;
}
#result{
color:#E30659;
font-size:21px;
font-family:Geneva, Arial, Helvetica, sans-serif;
}
</style>
After adding this CSS code,PHP Calculator looks same like image at top.You can also edit this CSS code to style calculator.
This is Our Project to make a Calculator in PHP. I hope you learn something from this article.

Start Work With Me

Contact Us
Anil
+91-7302739565
Dwarka Sector-7, New Delhi