Jump to content

Archived

This topic is now archived and is closed to further replies.

knight47

[Resolved] simple php script help

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites
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.php

edit, 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.

Share this post


Link to post
Share on other sites
if ($num1 == 0)
{
echo "you can't divide by 0";
}

Jack

Share this post


Link to post
Share on other sites
[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?

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.