PHP Script Question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Mman
Forum Newbie
Posts: 3
Joined: Mon Aug 18, 2003 8:16 pm

PHP Script Question

Post 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:
?>
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Re: PHP Script Question

Post 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.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
Last edited by twigletmac on Tue Aug 19, 2003 4:39 am, edited 1 time in total.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post 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"); 
} 

?>
Image Image
Mman
Forum Newbie
Posts: 3
Joined: Mon Aug 18, 2003 8:16 pm

Post 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 :)
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Another option (variable functions):

$action($number1, $number2);

.. cuts out the switching.

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