Jump to content

[SOLVED] Why am I getting a parsing error?


suttercain

Recommended Posts

Simple submit form and I am getting a parsing error:

 

<?php
extract ($_REQUEST);
if (! isset ($submit_age) or $age =="") {
print "You Must Submit Your Age...<br />";
exit;
}

if ($age < 21) {
print "Sorry, youre too young to drink!";
}
else ($age >= 21)
print "Cool, youre $age! Lets get fu__ed up!!!";
}
?>

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/42144-solved-why-am-i-getting-a-parsing-error/
Share on other sites

<?php
extract ($_REQUEST);
if (!isset($submit_age) || $age =="") {
print "You Must Submit Your Age...<br />";
exit;
}

if ($age < 21) {
print "Sorry, youre too young to drink!";
}
else ($age >= 21)
print "Cool, youre $age! Lets get fu__ed up!!!";
}
?>

 

or = ||

 

--FrosT

Did you do this?

 

<?php
extract ($_REQUEST);
if (!isset($submit_age) || $age =="") {
print "You Must Submit Your Age...<br />";
exit;
}

if ($age < 21) {
print "Sorry, youre too young to drink!";
}
else ($age >= 21) {
print "Cool, youre $age! Lets get fu__ed up!!!";
}
?>

 

Add the bracket on the else?

 

If so than please post the error that it gives you.

 

My bad barand, I forgot that php allows both. Thanks for correcting me.

 

--FrosT

Here is the current code:

<?php
extract ($_REQUEST);
if (! isset ($submit_age) or $age =="") {
print "You Must Submit Your Age...<br />";
exit;
}

if ($age < 21) {
print "Sorry, youre too young to drink!";
}
else ($age >= 21) {
print "Cool, youre $age! Lets get fu__ed up!!!";
}
?>

Here is the error message:
Parse error: parse error, unexpected '{' in D:\wamp\www\PHP Tutorials\By Example\examples\chapter7_examples\lab_1a.php on line 19

Both work, but as it says in the manual, precedence is slightly different.

 

Whenever I use || I always put the conditions in parentheses to avoid precendence problems, paticularly when mixing && and ||

 

EG

 

if (!isset($submit_age) || ($age =="") )

<?php
extract ($_REQUEST);
if (! isset ($submit_age) or $age =="") {
print "You Must Submit Your Age...<br />";
exit;
}

if ($age < 21) {
print "Sorry, youre too young to drink!";
}
else {
print "Cool, youre $age! Lets get fu__ed up!!!";
}
?>

 

OR

 

<?php
extract ($_REQUEST);
if (! isset ($submit_age) or $age =="") {
print "You Must Submit Your Age...<br />";
exit;
}

if ($age < 21) {
print "Sorry, youre too young to drink!";
}
elseif ($age > 20 {
print "Cool, youre $age! Lets get fu__ed up!!!";
}
?>

 

The else either has to be an elseif for the condition or just an else without a condition

 

to Barand,

 

That is actually a really good idea. I am going to have to use it. Thanks for the insight (I have had problems with that in the past)

 

--FrosT

@FrosT

 

It's particularly problematic when you have something like this

 

<?php
if (a == b && c == d || c == e)
?>

 

Do you really mean this  (default without the ()s )

 

<?php
if ( (a == b && c == d) || (c == e) )
?>

 

or do you mean this

 

<?php
if ( (a == b) && ( (c == d) || (c == e) ) )
?>

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.