Help with php syntax error

Hello,
i just wrote an example form a php book i was reading that parses an input field for the user to type their name and then be greeted.

I dont understand why i am getting a syntax error on line 16, because all that is at line 16 is the closing php syntax ?>
Here is the error specifically:
Parse error: syntax error, unexpected $end in /homepages/9/d197248293/htdocs/phpWelcome.php on line 16

And here is my php code:

<?php
if($_POST['user']) {
   print "Hello, ";
   print $_POST['user'];
   print "!";
   }
   else {
     print <<<_HTML_
	 <form method="post" action="$_SERVER[PHP_SELF]">
	 Username: <input type="text" name="user">
	 <br />
	 <input type="submit" value"Login">
	 </form>
	 _HTML_;
	 }
?>

I was wondering if someone could please help me fix this script because saying a specific welcome to the user is something I have been wanting to do in PHP for a while.

Thanks in advance and regards,
Team 1504

close the final brace

if($_POST['user']) {
   print "Hello, ";
   print $_POST['user'];
   print "!";
   }
 
   else {
print <<<_HTML_
     <form method="post" action="$_SERVER[PHP_SELF]">
     Username: <input type="text" name="user">
     <br />
     <input type="submit" value"Login">
     </form>
_HTML_;
}

The problem is the spaces before the last HTML try this


if($_POST['user']) {
   print "Hello, ";
   print $_POST['user'];
   print "!";
   }
   else {
print <<<_HTML_
     <form method="post" action="$_SERVER[PHP_SELF]">
     Username: <input type="text" name="user">
     <br />
     <input type="submit" value"Login">
     </form>
_HTML_;

if you have whitespaces or tab before the last HTML the simbol is diferent and not ends the heredoc string

The HTML; need to be on its own line, with nothing else in the same line

I am not frustrated with you all; in fact, I am very appreciative for your help!
I am frustrated with why this is not working

Here is the PHP code i have now:

<?php
if($_POST['user']) {
   print "Hello, ";
   print $_POST['user'];
   print "!";
   }

   else {
print <<<_HTML_
     <form method="post" action="$_SERVER[PHP_SELF]">
     Username: <input type="text" name="user">
     <br />
     <input type="submit" value"Login">
     </form>
_HTML_;
?>

And i get this error: Parse error: syntax error, unexpected $end in /homepages/9/d197248293/htdocs/phpWelcome.php on line 16

is it a problem with the closing syntax?

I believe i did everything i was suggested to do; including putting the print <<<HTML<form… line all on one line.

But when I upload that:

<?php
if($_POST['user']) {
   print "Hello, ";
   print $_POST['user'];
   print "!";
   }

   else {
/* all on one line */
print <<<_HTML_<form method="post" action="$_SERVER[PHP_SELF]">Username: <input type="text" name="user"><br /><input type="submit" value"Login"></form>
/*End all on one line */
_HTML_; 
?>

I get this error:Parse error: syntax error, unexpected $end in /homepages/9/d197248293/htdocs/phpWelcome.php on line 12

Thanks for all your help and thanks in advance, I hope you can help me get this seemingly simply script working

Regards,
Team 1504

now i am getting another error…
The server is parsing the error:

Parse error: syntax error, unexpected T_SL in /homepages/9/d197248293/htdocs/phpWelcome.php on line 8

And here is the edited code:

<?php
if($_POST['user']) {
   print "Hello, ";
   print $_POST['user'];
   print "!";
   }
   else {
/* This is all one line */
     print <<<_HTML_<form method="post"action="$_SERVER[PHP_SELF]">Username: <input type="text"name="user"><br /><input type="submit" value"Login"></form>_HTML_;
	 }
?>

This block of code:

print <<<_HTML_<form method="post" action="$_SERVER[PHP_SELF]">      Username: <input type="text" name="user">      <br />      <input type="submit" value"Login">      </form>      _HTML_; 

needs to be flush up against the left of the page, you’re not allowed to indent a heredoc block, from the manual:

Warning It is very important to note that the line with the closing identifier must contain no other characters, except possibly 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
on UNIX systems, including Mac OS X. The closing delimiter (possibly followed by a semicolon) 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 members. Since PHP 5.3, this limitation is valid only for heredocs containing variables.

ah okay.
Thank you very much space phoenix! :slight_smile:

And that does make sense as it is all one print line.