PDA

View Full Version : Parse Error



mrhoops515
05-06-2007, 09:35 PM
I lumped together this code after reading a few tutorials (quite new to PHP) and keep getting this error:

parse error, unexpected T_STRING, expecting ',' or ';' in play.php on line 9


<ul id="nav">
<?
echo'<li><a href="play.php?game=The Legend of Zelda"';
if($game=="The Legend of Zelda"){echo' class="current"';}
echo'>Legend of Zelda</a>
<li><a href="play.php?game=The Adventure of Link"';
if($game=="The Adventure of Link"){echo' class="current"';}
echo'>Adventure of Link</a></li>
<li><a href="play.php?game=Link's Awakening"';
if($game=="Link's Awakening"){echo' class="current"';}
echo'>Link's Awakening</a></li>
<li><a href="play.php?game=Ocarina of Time"';
if($game=="Ocarina of Time"){echo' class="current"';}
echo'>Ocarina of Time</a></li>
<li><a href="play.php?game=Majora's Mask"';
if($game=="Majora's Mask"){echo' class="current"';}
echo'>Majora's Mask</a></li>
<li><a href="play.php?game=Oracle of Seasons"';
if($game=="Oracle of Seasons"){echo' class="current"';}
echo'>Oracle of Seasons</a></li>
<li><a href="play.php?game=Oracle of Ages"';
if($game=="Oracle of Ages"){echo' class="current"';}
echo'>Oracle of Ages</a></li>
<li><a href="play.php?game=The Wind Waker"';
if($game=="The Wind Waker"){echo' class="current"';}
echo'>Wind Waker</a></li>
<li><a href="play.php?game=The Minish Cap"';
if($game=="The Minish Cap"){echo' class="current"';}
echo'>Minish Cap</a></li>
<li><a href="play.php?game=Twilight Princess"';
if($game=="Twilight Princess"){echo' class="current"';}
echo'>Twilight Princess</a></li>';
?>
</ul>


Any help would be much appreciated.

thetestingsite
05-06-2007, 09:48 PM
Try escaping the single quotes in the string like so:



<ul id="nav">
<?php
echo'<li><a href="play.php?game=The Legend of Zelda"';

if($game=="The Legend of Zelda"){echo' class="current"';}

echo'>Legend of Zelda</a>
<li><a href="play.php?game=The Adventure of Link"';

if($game=="The Adventure of Link"){echo' class="current"';}

echo'>Adventure of Link</a></li>
<li><a href="play.php?game=Link\'s Awakening"';

if($game=="Link's Awakening"){echo' class="current"';}

echo'>Link\'s Awakening</a></li>
<li><a href="play.php?game=Ocarina of Time"';

if($game=="Ocarina of Time"){echo' class="current"';}

echo'>Ocarina of Time</a></li>
<li><a href="play.php?game=Majora\'s Mask"';

if($game=="Majora's Mask"){echo' class="current"';}

echo'>Majora\'s Mask</a></li>
<li><a href="play.php?game=Oracle of Seasons"';

if($game=="Oracle of Seasons"){echo' class="current"';}

echo'>Oracle of Seasons</a></li>
<li><a href="play.php?game=Oracle of Ages"';

if($game=="Oracle of Ages"){echo' class="current"';}

echo'>Oracle of Ages</a></li>
<li><a href="play.php?game=The Wind Waker"';

if($game=="The Wind Waker"){echo' class="current"';}

echo'>Wind Waker</a></li>
<li><a href="play.php?game=The Minish Cap"';

if($game=="The Minish Cap"){echo' class="current"';}

echo'>Minish Cap</a></li>
<li><a href="play.php?game=Twilight Princess"';

if($game=="Twilight Princess"){echo' class="current"';}

echo'>Twilight Princess</a></li>';
?>
</ul>


Hope this helps.

Twey
05-06-2007, 09:49 PM
The cause of this error is the fact that you have a ' "inside" a quoted string. This breaks your syntax. Escape it by preceding it with a backslash (\).

However, when you find yourself repeating code like that, it's a sure sign you're doing something wrong. It's much neater to loop:
<?php
$links = array(
'The Legend of Zelda',
'The Adventure of Link',
'Link\'s Awakening',
'Ocarina of Time',
'Majora\'s Mask',
'Oracle of Seasons',
'Oracle of Ages',
'The Wind Waker',
'The Minish Cap',
'Twilight Princess'
);
?>
<ul>
<?php foreach($links as $link) { ?>
<li>
<a
<?php if($_GET['game'] === $link) print 'class="current"'; ?>
href="play.php?game=<?php print $link; ?>"
>
<?php print $link; ?>
</a>
</li>
<?php } ?>
</ul>

mrhoops515
05-06-2007, 09:56 PM
Thanks a lot! That array code is quite handy and it makes sense why the single quotes have to be escaped.

GhettoT
05-07-2007, 04:38 AM
You could also clean up your code by doing something like this:


<?php
$links = array(
'The Legend of Zelda',
'The Adventure of Link',
'Link\'s Awakening',
'Ocarina of Time',
'Majora\'s Mask',
'Oracle of Seasons',
'Oracle of Ages',
'The Wind Waker',
'The Minish Cap',
'Twilight Princess'
);

echo '<ul>';
foreach($links as $link) {
echo '<li>';
echo '<a';
if($_GET['game'] === $link) echo 'class="current"';
echo 'href="play.php?game='.$link.'>';

echo $link;
echo '</a>';
echo '</li>';
}
echo '</ul>'
?>

mwinter
05-07-2007, 10:50 AM
You could also clean up your code by doing something like this:

Any particular reason why you chose to more-or-less duplicate Twey's post, albeit with an unnecessary overuse of echo statements? :)

Mike

alexjewell
05-09-2007, 09:56 PM
Ghetto's post just structures the code a bit more. Although unnecessary, it may make more sense to someone more adapt to writing HTML. Just a guess, though, really. Ghetto?

Twey
05-10-2007, 11:12 AM
No, it doesn't structure the code any more. It also produces messier HTML output, and is (admittedly marginally) slower to parse than leaving PHP parsing mode.