metathirteen Posted April 26, 2009 Share Posted April 26, 2009 I'm really new to php. I've only been messing with it for a couple of days now, and i tried making my own calculator kinda thing but its not working right and i cant figure it out . could anyone help me out and tell me whats wrong and why its not working? 1st post on this forum too btw =D <html> <form action='calc.php' method='GET'> Number 1:<input type='text' name='num1'><br> OP:<input type='text' name='op'><br> Number 2:<input type='text' name='num2'><br> <input type='submit' value='Equals'> </form> </html> <?php $num1 = $_GET['num1']; $num2 = $_GET['num2']; $op = $_GET['op']; switch ($op) { case "+": $total = $num1 + $num2; return $total; break; case "-": $total = $num1 - $num2; return $total; break; case "*": $total = $num1 * $num2; return $total; break; case "/": $total = $num1 / $num2; return $total; break; default: echo "unknown operator"; } echo $total; } Quote Link to comment https://forums.phpfreaks.com/topic/155755-solved-new-to-php-calc-help/ Share on other sites More sharing options...
mikesta707 Posted April 26, 2009 Share Posted April 26, 2009 you dont really need to return $total, since you are assigning it a value (I'm pretty sure, someone please correct me if i'm wrong) but I dont know if using that return would mess up the script. however, you probably want to encase all your php in the following if statement if (isset($_GET['num1'] && isset($_GET['num2'] && isset($_GET['num3']){ ...your code } that way you dont run that switch statement if the user first accesses the page. Quote Link to comment https://forums.phpfreaks.com/topic/155755-solved-new-to-php-calc-help/#findComment-819862 Share on other sites More sharing options...
premiso Posted April 26, 2009 Share Posted April 26, 2009 Do not do return $total, you should use return more or less for methods and functions. You are not using it in the way you think. Remove the return's and see if that fixes it. You also have an extra closing brace ( } ) and next time explain how it is not working. It will result in better help. Quote Link to comment https://forums.phpfreaks.com/topic/155755-solved-new-to-php-calc-help/#findComment-819863 Share on other sites More sharing options...
metathirteen Posted April 27, 2009 Author Share Posted April 27, 2009 awesome i put in the if code, i wasnt really sure at first what it was for but now i do lol. thanks mikesta707. and i took out the returns and its working just fine now.at first it wasnt but then i saw it said get num3, i switched it to op and then it worked lol. thanks for the help guys. sorry i didnt really explain what it was doing. when i 1st tried it, id put in the numbers and the op and after i hit submit the get part worked, everything was coming up in the URL ok but it wasnt echoing out anything and i wasnt sure if it was even calculating anything. thanks for the help guys, i appreciate it =D i might be in here alot lol Quote Link to comment https://forums.phpfreaks.com/topic/155755-solved-new-to-php-calc-help/#findComment-819942 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.