Question:
PHP ELSEIF Parse Error. What is causing this?
Danny B
2009-02-08 10:49:31 UTC
I receive the following error for this code.

Parse error: parse error in F:\xampplite-win32-1.7.0\xampplite\htdocs\d\7-1b.php on line 26

Note: I can comment out all the elseifs and the code will work, it will give me the expected results for A and it will give me the expected error message if one of my values is not numeric. I am confused as to why I receive parse errors upon moving into my elseifs. Also even were I to change the elseifs to just regular ifs I still receive the same error.

extract($_REQUEST, EXTR_SKIP);
if (is_numeric($g1) and is_numeric($g2) and is_numeric($g3))
{
$gAve = (round(($g1 + $g2 + $g3)/3));
if ($gAve >= 90)
{
print("

$gAve A

");
}
elseif ($gAve >=80 and <=89)
{
print("

$gAve B

");
}
elseif ($gAve >=70 and <=79)
{
print("

$gAve C

");
}
elseif ($gAve >=60 and <=69)
{
print("

$gAve D

");
}
elseif ($gAve <=59 and >=0)
{
print("
$gAve F
You must not fail!!! DO BETTER!!!!!!
");
}
else
{
print("negative numbers are not acceptable entries");
}

}
else
{
print ("please enter numeric data only");
}
?>
Five answers:
David D
2009-02-08 10:54:43 UTC
elseif ($gAve >=80 and <=89)



So, if gAve is greater than or equal to 80 AND ... is less than or equal to 89.



WHAT is less than or equal to that?



Each condition is independent, the left hand side of the condition doesn't automatically appear in the next one.
CompanionCube
2009-02-08 10:57:18 UTC
You are very close.



elseif ($gAve <=59 and >=0)



should actually read



elseif ($gAve <=59 and $gAve >=0)



Note the reinsertion of the variable. Fix this in each of your elseif lines and you are all set. May also just be me, but I typically use echo instead of print... don't need all those extra parentheses.



echo "

$gAve D

";
menotu3169
2009-02-08 10:59:00 UTC
I don't actually know any PHP, but I do know a few other lanuages, so this is just a guess but shouldn't



elseif ($gAve >=80 and <=89)



be



elseif ($gAve >=80 and $gAve <=89)
2009-02-08 14:16:10 UTC
You've got your answers above. To clarify: In any expression involving a variable the variable identifier must be specifically given e.g.



If x >90 and <100

will not be accepted in any language it must be



If x>90 and x<100
rehrer
2016-12-14 19:30:50 UTC
that's an exceedingly straightforward restoration, and that i'm able to work out the way you're able to omit it after observing it for too long. first of all of the logical "and" operator in Hypertext Preprocessor is "&&" so which you're able to write: elseif ($a > 5 && $a <= 10) echo "prepare some comments and furnish them with sincerity"; elseif ($a > a million && $a <= 5) echo 'you're able to a minimum of furnish a perfunctory "sorry"'; or in case you needed a logical "or" interior the 2d elseif then you certainly could write: elseif ($a > 5 && $a <= 10) echo "prepare some comments and furnish them with sincerity"; elseif ($a > a million || $a <= 5) echo 'you're able to a minimum of furnish a perfunctory "sorry"'; ......................................... additionally, as solid programming prepare, positioned areas between your tokens. somewhat of: if($a>10) write: if($a > 10) and do the comparable with something. areas delineate tokens. If the interpreter or compiler would not come across an area between tokens, it is going to insert the optimal amout of areas immediately. as a result, that's counter efficient to depart out areas. P.S. your expenses have been effective lol. in case you have been to do it the way that the 1st guy reported, then you certainly could be employing the fees impropperly. it may artwork, yet single quote interior double expenses denote a variable is between them. Double expenses interior single expenses are in basic terms component to the literal.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...