Results 1 to 8 of 8

Thread: Parse Error

  1. #1
    Join Date
    Sep 2006
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Parse Error

    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

    Code:
    <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.

  2. #2
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Try escaping the single quotes in the string like so:

    Code:
    <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.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    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:
    Code:
    <?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>
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #4
    Join Date
    Sep 2006
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks a lot! That array code is quite handy and it makes sense why the single quotes have to be escaped.

  5. #5
    Join Date
    Nov 2006
    Posts
    99
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    You could also clean up your code by doing something like this:
    PHP Code:
    <?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>'
    ?>

  6. #6
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by GhettoT View Post
    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

  7. #7
    Join Date
    Mar 2006
    Location
    Cleveland, Ohio
    Posts
    574
    Thanks
    6
    Thanked 5 Times in 5 Posts

    Default

    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?
    Thou com'st in such a questionable shape
    Hamlet, Act 1, Scene 4

  8. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    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.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •