0

I am writing a code that contains HTML as well as PHP commands. When I try to run the code on my browser, I get this message :

Parse error: syntax error, unexpected $end in C:\WAMP\www\urr00001\SummerProject\projectquestionnaire.php on line 21.

I don't understand why line 21 is wrong. I was under the assumption that the "echo <<<_END ... _END" construct must be used whenever multiline HTML must be output. But the browser does not like this. My code is shown below. How can I correct this situation, please?

      <?php //questionnaire.php
           if (isset($_POST['name'])) $name = $_POST['name'];
           else $name = "(Not entered)";

          echo <<<_END
            <html>
              <head>
                <title>Questionnaire</title>
              </head>
              <body>
                Your name is: $name<br>
                <form method="post"  action="questionnaire.php">
                  What is your name?
                  <input type="text"  name="name">
                  <input type="submit">
              </form>
            </body>
         </html>
      _END;
    ?>        
1
  • 3
    If you have spaces before and/or after _END; remove them, which is most likely the problem. Commented Jul 3, 2014 at 19:47

2 Answers 2

1

It's confirmed, you do have spaces before _END; (remove them)

Use this: (exactly as posted)

<?php //questionnaire.php
           if (isset($_POST['name'])) $name = $_POST['name'];
           else $name = "(Not entered)";

          echo <<<_END
            <html>
              <head>
                <title>Questionnaire</title>
              </head>
              <body>
                Your name is: $name<br>
                <form method="post"  action="questionnaire.php">
                  What is your name?
                  <input type="text"  name="name">
                  <input type="submit">
              </form>
            </body>
         </html>
_END;
?>

Consult heredoc syntax

Warning It is very important to note that the line with the closing identifier must contain no other characters, except a semicolon (;). That means especially that the identifier may not be indented, and there may not be any spaces or tabs before or after the semicolon. It's also important to realize that the first character before the closing identifier must be a newline as defined by the local operating system. This is \n on UNIX systems, including Mac OS X. The closing delimiter must also be followed by a newline. If this rule is broken and the closing identifier is not "clean", it will not be considered a closing identifier, and PHP will continue looking for one. If a proper closing identifier is not found before the end of the current file, a parse error will result at the last line. Heredocs can not be used for initializing class properties. Since PHP 5.3, this limitation is valid only for heredocs containing variables.


Allowed: (no spaces)

         </html>
_END;
?>

Not allowed: (1 or more spaces)

         </html>
 _END;
^--// that's a space, and you have many.
?>
Sign up to request clarification or add additional context in comments.

1 Comment

Fred, thank you, it works fine now. I tried to vote you up, but I don't have enough reputation to do so.
0

This is just an opinion, but I would much rather see this in the code:

  **<?php //questionnaire.php
       if (isset($_POST['name'])) $name = $_POST['name'];
       else $name = "(Not entered)";
  ?>**
       <html>
          <head>
            <title>Questionnaire</title>
          </head>
          <body>
            Your name is: **<?php echo $name;?>**<br>
            <form method="post"  action="questionnaire.php">
              What is your name?
              <input type="text"  name="name">
              <input type="submit">
          </form>
        </body>
     </html>

Because it lets others know (especially those who know HTML but not PHP) exactly where PHP is used and where the file is only HTML and let's them see it at a glance. Your file would still be named .php, by the way. Of course, if your project is requiring "echo <<<_END..._END;" then you have to use that.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.