Jump to content


Photo

[Resolved] simple php script help


  • Please log in to reply
5 replies to this topic

#1 knight47

knight47
  • Members
  • PipPipPip
  • Advanced Member
  • 50 posts

Posted 28 October 2006 - 04:04 AM

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: www.knight47.com/php_scripts/add.html

the code:
<?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;
}
?>

when I remove this part of the code:

else ($_POST['Submit2'])
{
echo "$num1 * $num2 = ";
echo $num1 * $num2;
}

it works perfectly, but for some reason when it's in there it won't work, and gives this error:

Parse error: syntax error, unexpected '{' in /home/sbai/public_html/php_scripts/add.php on line 13

Line 13 is { right under else... Anyone know what's going on?

??? ???

Thanks!

#2 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 28 October 2006 - 04:25 AM

You need to take a look at the syntax for else. Here.

You could use....

if ($_POST['Submit1']) 
{
  echo "$num1 + $num2 = ";
  echo $num1 + $num2;
}

if ($_POST['Submit2'])
{
  echo "$num1 * $num2 = ";
  echo $num1 * $num2;
}
?>


#3 jrcarr

jrcarr
  • Members
  • PipPip
  • Member
  • 20 posts
  • LocationBeautiful Arkansas Ozarks

Posted 28 October 2006 - 04:31 AM

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;
}

Jack Carr

#4 knight47

knight47
  • Members
  • PipPipPip
  • Advanced Member
  • 50 posts

Posted 28 October 2006 - 04:43 AM

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.

#5 jrcarr

jrcarr
  • Members
  • PipPip
  • Member
  • 20 posts
  • LocationBeautiful Arkansas Ozarks

Posted 28 October 2006 - 02:25 PM

if ($num1 == 0)
{
echo "you can't divide by 0";
}

Jack
Jack Carr

#6 knight47

knight47
  • Members
  • PipPipPip
  • Advanced Member
  • 50 posts

Posted 28 October 2006 - 04:53 PM

if ($num1 == 0)
{
echo "you can't divide by 0";
}

Jack

but then I wouldn't be able to add 0, subtract 0, and multiply by 0 right?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users