The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

More PHP problems....

oniianoniian Registered User regular
edited June 2007 in Help / Advice Forum
Hi guys,
So I am writing a PHP script that takes your marriage status and the amount of taxable income you have and returns the amount you owe. The rate you are tax on is based on three tiers of income. If you are married then the applicable tiers are at higher levels. Fairly simple right?

Problem: Following the books example of proper syntax for the statments used, I believe I have coded the PHP page correctly. But when I run it, it tells me "PHP Parse error: syntax error, unexpected '{' in C:\... on line 22 ". I rewrote it another way trying to trouble shoot it and got "PHP Parse error: syntax error, unexpected T_VARIABLE in C:\... on line 17".

Question: Looking at the two different scripts, do you see why I am getting these errors? Also in PHP ver. 5, has the IF... Elseif... Else been deprecated? I have ran into troubles also saying the "Elseif" doesn't works as a statement.

Here is the first code I was talking about. This is the one I got the " unexpected '{' " error in:
<?PHP
$Status = $_GET;
$TaxableIncome = $_GET;
$TaxRate1 = 0.15;
$TaxRate2 = 0.28;
$TaxRate3 = 0.31;
$AmountOwed;

switch ($status) {
case "single":
if ($TaxableIncome < 21450.00) {
$AmountOwed = $TaxableIncome * $TaxRate1;
return $AmountOwed;
}
elseif ($TaxableIncome > 21450.00 && $TaxableIncome < 51900.00) {
$AmountOwed = ($TaxableIncome - 21450.00) * $TaxRate2 + 3217.50;
return $AmountOwed;
}
else ($TaxableIncome > 51900.00) {
$AmountOwed = ($TaxableIncome - 51900.00) * $TaxRate3 + 11743.50;
return $AmountOwed;
}
break;
case "married":
if ($TaxableIncome < 35800.00) {
$AmountOwed = $TaxableIncome * $TaxRate1;
return $AmountOwed;
}
elseif ($TaxableIncome > 35800.00 && $TaxableIncome < 86500.00) {
$AmountOwed = ($TaxableIncome - 35800.00) * $TaxRate2 + 5370.00;
return $AmountOwed;
}
else ($TaxableIncome > 86500.00) {
$AmountOwed = ($TaxableIncome - 86500.00) * $TaxRate3 + 19566.00;
return $AmountOwed;
}
break;
default:
echo "Please select marriae status.";
}
?>


Here is the result of the trouble shooting that renders a error of "unexpected T_VARIABLE" (this one lacks polish):
<?PHP
$Status = $_GET;
$TaxableIncome = $_GET;
$TaxRate1 = 0.15;
$TaxRate2 = 0.28;
$TaxRate3 = 0.31;
$AmountOwed = 0;

if ($status == "single") {
if ($TaxableIncome < 21450.00) {
$AmountOwed = $TaxableIncome * $TaxRate1;
}
else ($TaxableIncome > 21450.00 && $TaxableIncome < 51900.00)
$AmountOwed = ($TaxableIncome - 21450.00) * $TaxRate2 + 3217.50;

else
if ($TaxableIncome < 35800.00)
global $AmountOwed;
$AmountOwed = $TaxableIncome * $TaxRate1;
elseif ($TaxableIncome > 35800.00 && $TaxableIncome < 86500.00)
global $AmountOwed;
$AmountOwed = ($TaxableIncome - 35800.00) * $TaxRate2 + 5370.00;
else ($TaxableIncome > 86500.00)
global $AmountOwed;
$AmountOwed = ($TaxableIncome - 86500.00) * $TaxRate3 + 19566.00;

?>

oniian on

Posts

  • telcustelcus Registered User regular
    edited June 2007
    Umm the first one looks like your problem is that you've declared a condition for your else.
    That is, "else ($TaxableIncome > 51900.00)"

    else statements don't need a condition, so PHP is parsing that just as a command, which will break your code because it isn't followed by a delimiter.

    The second one looks like your missing some curly braces before and after the else statement on line 16 and at the end.

    telcus on
    [SIGPIC][/SIGPIC]
  • GeodGeod swim, swim, hungryRegistered User regular
    edited June 2007
    telcus wrote: »
    Umm the first one looks like your problem is that you've declared a condition for your else.
    That is, "else ($TaxableIncome > 51900.00)"

    else statements don't need a condition, so PHP is parsing that just as a command, which will break your code because it isn't followed by a delimiter.

    The second one looks like your missing some curly braces before and after the else statement on line 16 and at the end.

    Yeah, that's what I see too.

    Geod on
  • oniianoniian Registered User regular
    edited June 2007
    Hey thanks for responding, and I thought this was dead in the water.

    I think I need sometime away from the code. I came back to it a couple hours later and I found two other errors. The status declaration and implementation didn't match caps-wise and the amount owed need to be initialized and not just declared. Now I just need to run and match the results. I'll repost if I get into some trouble.

    oniian on
Sign In or Register to comment.