darkside92 Posted February 25, 2009 Share Posted February 25, 2009 For Class (high school) i need to make a simple php calcualtor i have it made just cant figure out this error neither can anyone else =/ heres the error Parse error: syntax error, unexpected T_VARIABLE in C:\xampp\htdocs\php_sandbox\math2.php on line 29 <html> <head> <title>Untitled Document</title> </head> <body> <html> <body> <form action="$_PHP[SELF]" method="get"> <input type="text" name="num1" /> <select name="sign"> <option value="*">*</option> <option value="-">-</option> <option value="+">+</option> </select> <input type="text" name="num2" /> <input type="submit" /> </form> <?php if($_GET[sign]) { $val = $_GET[$num1] $_GET[$sign] $_GET[$num2]; echo $val; } ?> </body> </html> </body> </html> Quote Link to comment Share on other sites More sharing options...
dr0p Posted February 25, 2009 Share Posted February 25, 2009 $val = $_GET[$num1] $_GET[$sign] $_GET[$num2]; That statement has no logic whatsoever, you'd have to do something like extract($_GET); if ($sign == "+") { $val = $num1 + $num2; } elseif ($sign == "-") { $val = $num1 - $num2; } etc... Quote Link to comment Share on other sites More sharing options...
digip Posted February 25, 2009 Share Posted February 25, 2009 <html> <head> <title>PHP Calculator</title> </head> <body> <form action=<?php echo $_SERVER['PHP_SELF']; ?> method="POST"> <input type="text" name="num1" /> <select name="sign"> <option value="*">*</option> <option value="/">/</option> <option value="-">-</option> <option value="+">+</option> </select> <input type="text" name="num2" /> <input type="submit" /> </form> <?php $multi = "*"; $plus = "+"; $min = "-"; $div = "/"; if($_POST['sign'] == $multi) { $val = ($_POST['num1'] * $_POST['num2']); echo $_POST['num1'].$_POST['sign'].$_POST['num2']."=".$val; } if($_POST['sign'] == $plus) { $val = ($_POST['num1'] + $_POST['num2']); echo $_POST['num1'].$_POST['sign'].$_POST['num2']."=".$val; } if($_POST['sign'] == $min) { $val = ($_POST['num1'] - $_POST['num2']); echo $_POST['num1'].$_POST['sign'].$_POST['num2']."=".$val; } if($_POST['sign'] == $div) { $val = ($_POST['num1'] / $_POST['num2']); echo $_POST['num1'].$_POST['sign'].$_POST['num2']."=".$val; } ?> </body> </html> I do things the long way, but yeah, it still works. Clean it up and make one function instead of 4. Quote Link to comment Share on other sites More sharing options...
darkside92 Posted February 27, 2009 Author Share Posted February 27, 2009 thanxs dr0p and digip =D yea ill try to clean up the code im not that good at php i just started. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.