knight47 Posted October 28, 2006 Share Posted October 28, 2006 so I'm just experementing with PHP, very simple stuff. I tried to make a script that would add to numbers, it worked. But when I tried to add a multiplying option to the original adding option, I get an error.The link: [url=http://www.knight47.com/php_scripts/add.html]www.knight47.com/php_scripts/add.html[/url]the code:[code=php:0]<?php$num1 = $_POST['num1']; $num2 = $_POST['num2'];if ($_POST['Submit1']) {echo "$num1 + $num2 = ";echo $num1 + $num2;}else ($_POST['Submit2']){echo "$num1 * $num2 = ";echo $num1 * $num2;}?>[/code]when I remove this part of the code:[code=php:0]else ($_POST['Submit2']){echo "$num1 * $num2 = ";echo $num1 * $num2;}[/code]it works perfectly, but for some reason when it's in there it won't work, and gives this error:[B]Parse error: syntax error, unexpected '{' in /home/sbai/public_html/php_scripts/add.php on line 13[/B]Line 13 is [B]{[/B] right under else... Anyone know what's going on? ??? ???Thanks! Link to comment https://forums.phpfreaks.com/topic/25377-resolved-simple-php-script-help/ Share on other sites More sharing options...
trq Posted October 28, 2006 Share Posted October 28, 2006 You need to take a look at the syntax for [i]else[/i]. [url=http://au2.php.net/manual/en/control-structures.else.php]Here[/url].You could use....[code=php:0]if ($_POST['Submit1']) { echo "$num1 + $num2 = "; echo $num1 + $num2;}if ($_POST['Submit2']){ echo "$num1 * $num2 = "; echo $num1 * $num2;}?>[/code] Link to comment https://forums.phpfreaks.com/topic/25377-resolved-simple-php-script-help/#findComment-115711 Share on other sites More sharing options...
jrcarr Posted October 28, 2006 Share Posted October 28, 2006 Change your Else like the following:<?php$num1 = $_POST['num1']; $num2 = $_POST['num2'];if ($_POST['Submit1']) {echo "$num1 + $num2 = ";echo $num1 + $num2;}elseif ($_POST['Submit2']){echo "$num1 * $num2 = ";echo $num1 * $num2;}?>when I remove this part of the code:else ($_POST['Submit2']){echo "$num1 * $num2 = ";echo $num1 * $num2;} Link to comment https://forums.phpfreaks.com/topic/25377-resolved-simple-php-script-help/#findComment-115713 Share on other sites More sharing options...
knight47 Posted October 28, 2006 Author Share Posted October 28, 2006 You guys are awesome! Thank you very much! :)Edit, one more question if it's not too much...How would I make the output follow the = sign, instead of take me to a different page.www.knight47.com/php_scripts/math.phpedit, never mind got it! I love PHP!edit again, I hope you guys don't mind!I was trying to fix a bug, when you divide by 0, it gives you an error, so what I did was put another if statement that looked like this:if $num1 / 0 {echo "you can't divide by 0";}but that didn't work. It seems logical though. What do I need to do to prevent the user from dividing by 0?Thanks again. Link to comment https://forums.phpfreaks.com/topic/25377-resolved-simple-php-script-help/#findComment-115716 Share on other sites More sharing options...
jrcarr Posted October 28, 2006 Share Posted October 28, 2006 if ($num1 == 0) {echo "you can't divide by 0";}Jack Link to comment https://forums.phpfreaks.com/topic/25377-resolved-simple-php-script-help/#findComment-115825 Share on other sites More sharing options...
knight47 Posted October 28, 2006 Author Share Posted October 28, 2006 [quote author=jrcarr link=topic=113007.msg459037#msg459037 date=1162045543]if ($num1 == 0) {echo "you can't divide by 0";}Jack[/quote]but then I wouldn't be able to add 0, subtract 0, and multiply by 0 right? Link to comment https://forums.phpfreaks.com/topic/25377-resolved-simple-php-script-help/#findComment-115927 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.