Page 1 of 1

PHP Script Question

Posted: Mon Aug 18, 2003 8:16 pm
by Mman
Before you speak, please know that I'm very knew to PHP. I'm what you could called a 'n00b', but I prefer Scripter In Training.

This is a script that takes user input and calculates it. Basically it's a simple calculator script.Anyhow, I wrote this script the other day but have been having problems with it:

Code: Select all

<?php

if ( $Action = Add ) &#123;
$Result = $Number1 + $Number2
	print ("$Number1 plus $Number2 is $Result");
&#125;

if ( $Action = Subtract ) &#123;
$Result = $Number1 - $Number2
	print ("$Number1 minus $Number2 is $Result");
&#125;

if ( $Action = Multiply ) &#123;
$Result = $Number1 * $Number2
	print ("$Number1 plus $Number2 is $Result");
&#125;
else
&#123;
$Result = $Number1/$Number2
	print ("$Number1 divided by $Number2 is $Result");
&#125;

?>
At first, it came up with a parse error on line six, because the I forget the ';' after the print () function. However, now it is producing a parse error on line 23 or 29 I think it was. I really don't know what's wrong. It seems fine to me.

Help?

Mman :cry:
?>

Re: PHP Script Question

Posted: Mon Aug 18, 2003 8:30 pm
by McGruff
In an if clause, you need to use '==' not '='. In php '=' doesn't mean 'equals' : $var = $value; means 'assign this $value to $var'.

You're also missing some ';' to terminate statements.

Posted: Tue Aug 19, 2003 4:33 am
by twigletmac
McGruff pointed out the errors in your code. Perhaps for what you are doing a switch statement would make more sense?

Code: Select all

<?php

switch ($Action) {
	case 'Add':
		$Result   = $Number1 + $Number2;
		$Operator = 'plus';
		break;
	case 'Subtract':
		$Result   = $Number1 - $Number2;
		$Operator = 'minus';
		break;
	case 'Multiply':
		$Result   = $Number1 * $Number2;
		$Operator = 'multiplied by';
		break;
	case 'Divide':
		$Result   = $Number1 / $Number2;
		$Operator = 'divided by';
		break;
	default:
		$Result   = $Number1 + $Number2;
		$Operator = 'plus';
}

print $Number1.' '.$Operator.' '.$Number2.' is '.$Result;

?>
Mac

Posted: Tue Aug 19, 2003 4:34 am
by twigletmac
P.S. What version of PHP are you using and how are you accessing the user's input and where does it come from (i.e. post or get)?

Mac

Posted: Tue Aug 19, 2003 9:13 am
by phice
Also, inside if statements, unless you're comparing the variable to a number (only), you'll need to put quotations ("") around the compared value.

your fixed code:

Code: Select all

<?php 

if ( $Action == "Add" ) { 
$Result = $Number1 + $Number2 
   print ("$Number1 plus $Number2 is $Result"); 
} 

if ( $Action == "Subtract" ) { 
$Result = $Number1 - $Number2 
   print ("$Number1 minus $Number2 is $Result"); 
} 

if ( $Action == "Multiply" ) { 
$Result = $Number1 * $Number2 
   print ("$Number1 plus $Number2 is $Result"); 
} else { 
$Result = $Number1/$Number2 
   print ("$Number1 divided by $Number2 is $Result"); 
} 

?>

Posted: Tue Aug 19, 2003 8:14 pm
by Mman
OK. I understand now. I believe I'm using PHP v4.3.2. Anyways, I like the switch one with the case. The user input is coming via HTML Form using POST.

Thanks everyone!

Mman :)

Posted: Wed Aug 20, 2003 10:59 am
by McGruff
Another option (variable functions):

$action($number1, $number2);

.. cuts out the switching.

Define a fn for each case, function names = $action cases.