Learn how to create a calculator in PHP:
- Open your favorite text editor and copy the following code.
<?php
error_reporting(E_ALL ^ E_NOTICE);
$ans=0;
$val1=$_GET['val1'];
$val2=$_GET['val2'];
$opt=$_GET['opt'];
$ans=$_GET['ans'];
if(isset($_GET['submit']))
{
Switch($_GET['submit'])
{
case "+":
$opt="+";
break;
case "-":
$opt="-";
break;
case "*":
$opt="*";
break;
case "/":
$opt="/";
break;
case "%":
$opt="%";
break;
case "C":
$val1=0;
$val2=0;
$opt="";
$ans="";
$flag=false;
break;
case "=":
$flag=true;
switch($opt)
{
case "+":
$ans=$val1+$val2+$ans;
$val1=0;
$val2=0;
$opt="";
break;
case "-":
if($val1==0){
$ans=($ans-$val2);
$val1=0;
$val2=0;
$opt="";
break;
}else if ($ans==0){
$ans=($val1-$val2);
$val1=0;
$val2=0;
$opt="";
break;
}
case "*":
if($val1==0){
$ans=$val2*$ans;
$val1=0;
$val2=0;
$opt="";
break;
}
else if($ans==0 ){
$ans=1;
$ans=$val1*$val2*$ans;
$val1=0;
$val2=0;
$opt="";
break;
}else{
$ans=$val1*$val2*$ans;
$val1=0;
$val2=0;
$opt="";
break;
}
case "/":
if($val1==0)
{ $ans=1;
$ans=($ans)/($val2);
$val1=0;
$val2=0;
$opt="";
break;
}
else
{ $ans=1;
$ans=($ans)/($val1/$val2);
$val1=0;
$val2=0;
$opt="";
break;
}
case "%":
if($val1==0)
{$ans=1;
$ans=($ans%$val2);
$val1=0;
$val2=0;
$opt="";
break;
}else{
$ans=1;
$ans=$ans($val1%$val2);
$val1=0;
$val2=0;
$opt="";
break;
}
}
default :
if($opt=="" and $flag==false)
$val1=$_GET['val1']*10+$_GET['submit'];
else
if($flag==false)
$val2=$_GET['val2']*10+$_GET['submit'];
}
}
//echo "</br> val1=$val1 ($opt) val2=$val2 ans=$ans";
//$a=$ans;
?>
<html>
<body>
<form method="get">
<input type="hidden" name="val1" value="<?php echo $val1 ?>" />
<input type="hidden" name="val2" value="<?php echo $val2 ?>" />
<input type="hidden" name="opt" value="<?php echo $opt ?>" />
<input type="hidden" name="ans" value="<?php echo $ans ?>" />
<table align="Center" border="1" color="Red">
<tr>
<td align = "center" colspan="5"> PHP Calculator </td>
</tr>
<tr>
<td align = "center" colspan="5" >
<input type="text" name="display" value="<?php if($flag==true) echo $ans; else echo($opt==""?$val1:$val2 );?>" />
</td></tr>
<tr>
<td> <input type="submit" value="7" name="submit"></td>
<td> <input type="submit" value="8" name="submit"></td>
<td> <input type="submit" value="9" name="submit"></td>
<td> <input type="submit" value="/" name="submit"></td>
<td><input type="submit" value="C" name="submit"></td>
</tr>
<tr>
<td> <input type="submit" value="4" name="submit" ></td>
<td> <input type="submit" value="5" name="submit"></td>
<td> <input type="submit" value="6" name="submit"></td>
<td> <input type="submit" value="*" name="submit"></td>
<td rowspan="3"><input type="submit" value="=" name="submit" ></td>
</tr>
<tr>
<td> <input type="submit" value="1" name="submit"></td>
<td> <input type="submit" value="2" name="submit"></td>
<td> <input type="submit" value="3" name="submit"></td>
<td> <input type="submit" value="%" name="submit"></td>
</tr>
<tr>
<td> <input type="submit" value="0" name="submit" ></td>
<td> <input type="submit" value="." name="submit"></td>
<td> <input type="submit" value="+" name="submit"></td>
<td> <input type="submit" value="-" name="submit"></td>
</tr>
</table>
</form>
</body>
</html>
- Past this code and save this file as index.php
- Run this file.
try using a syntax highlighter like prettify or alexgorbatchev
ReplyDelete8/2=0.25
ReplyDeleteplease correct this error