Getting a parse error. Nothing seems wrong. Help!

Discussion in 'PHP' started by Daedalus4198, Sep 25, 2008.

  1. #1
    I'm working on a basic site for a beginner-level web design class I'm in, and I'm using some of my php knowledge to go above and beyond, so to speak. However, I'm having some problems.

    My index page has a simple post form that asks the user's name. Simple enough. I have the page the submit button links to (welcome.php) set $_POST to $_COOKIE["name"]. That works well enough for my purposes. I then edited the index page to include an if/else statement so it doesn't display the input form if the cookie is set. This is where my problems start.

    Here's the offending chunk of code.

    <?php 
    if (isset ($_COOKIE["name"]))
      {
        echo "Welcome back," . $_COOKIE["name"] .  ". Click <a href="welcome.php">Here</a> to continue." ; 
        }
     else
     {
    echo "<form action="welcome.php" method="cookie">Please enter your name: <input type="text" name="name" /><input type="submit"/></form>";
    }
    ;
    ?>
    PHP:
    I know my syntax is probably really gross, but I'm a beginner. My problem is that I keep getting a parse error on the "Welcome back," line. The error is:

    I have a semicolon after the echo there, you can see it for yourself. Any ideas? I'm completely lost.
     
    Daedalus4198, Sep 25, 2008 IP
  2. Ikki

    Ikki Peon

    Messages:
    474
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    0
    #2
    HI there,

    Try this:

    
    <?php
    if (isset ($_COOKIE["name"]))  {
        echo "Welcome back," . $_COOKIE["name"] .  ". Click <a href='welcome.php'>Here</a> to continue." ;
         } else {
        echo "<form action='welcome.php' method='cookie'>Please enter your name: <input type='text' name='name' /><input type='submit'/></form>";
    }
    ?>
    
    PHP:
    You can't use quotes inside quotes. Instead, you could either escape them (as suggested by lp1051 below) or replace them with single quotes (').
     
    Ikki, Sep 25, 2008 IP
    Daedalus4198 likes this.
  3. lp1051

    lp1051 Well-Known Member

    Messages:
    163
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    108
    #3
    You need to escape the quotes inside quotes, if you want do echo quote (visit php.net):

    echo "Welcome back," . $_COOKIE["name"] . ". Click <a href=\"welcome.php\">Here</a> to continue." ;
    echo "<form action=\"welcome.php\" method=\"cookie\">Please enter your name: <input type=\"text\" name=\"name\" /><input type=\"submit\"/></form>";
     
    lp1051, Sep 25, 2008 IP
    Daedalus4198 likes this.
  4. Daedalus4198

    Daedalus4198 Guest

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Oh, I see now. That makes sense. Thanks guys!
     
    Daedalus4198, Sep 25, 2008 IP
  5. h0ly lag

    h0ly lag Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Another thing that PHP lets you do is allow you to close and re open php tags as if the script never ended. For example:

    <?php
    echo "
    ?>
    some html here
    <a href="#">link</a>
    <?php
    ";
    //continuing the script here
    if ($blabla) {
    echo "foobar";
    }
    ?>
    PHP:
    Is the same as:

    <?php
    echo "some html here
    <a href=\"#\">link</a>";
    
    //continuing the script here
    if ($blabla) {
    echo "foobar";
    }
    ?>
    PHP:
     
    h0ly lag, Sep 25, 2008 IP
  6. fireworking

    fireworking Peon

    Messages:
    460
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thanks. Escaping is a very good tool that I have recently thoguht of. I spent 5 hours of trying to debug something while in the book right next to me teaches escaping quotes in the second Chapter!
     
    fireworking, Sep 25, 2008 IP