Jump to content

[Resolved] simple php script help


knight47

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!
Link to comment
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]
Link to comment
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;
}
Link to comment
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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.