easy php code fix please!

Appledrop

Platinum Member
Aug 25, 2004
2,340
0
0
Uh huh, fixed, thanks guys


hi, new to php, am trying to make a very simple calculator, the user submits two numbers and an operator type (+,*,-, or /)... heres my code in calc.php (attached)

please see whats wrong.. the page is at

HERE IS THE PAGE!


<?php
$1st=$_POST["1st"];
$2nd=$_POST["2nd"];
$operator=$_POST["ope"];

function calculate($one,$two,$sign) { //start function
settype($one,'double');
settype($two,'double');

if(($sign=="/")) {
$total=($one/$two); }
else if(($sign=="*")) {
$total=($one*$two); }
else if(($sign=="+")) {
$total=($one+$two); }
else if(($sign=="-")) {
$total=($one-$two); }
else { $total=0;
echo "Wrong operator type!";
}
return $total; }
//end function
if((!empty==$ope)) {
echo "$1st $ope $2nd equals .";
echo calculate($1st,$2nd,$ope);
}
else { echo "an error occured"; }
?>


thanks
 

Transition

Banned
Sep 8, 2001
2,615
0
0
Might want to try using case statements instead of if/else. Makes the code a whole lot more readable.