Heredoc PHP error: "Parse error: syntax error, unexpected end of file"

1

Reviewing a code, I found something in Heredoc notation:

<?php

$html = <<<XXX
            <script type="text/javascript">
            ...
            </script>
XXX; //must be begin in column 0 or ERR

echo $html;

This code, when executed, produces an error and I do not know why:

Parse error: syntax error, unexpected end of file in D:\PHP\heredoc.php on line 10
  • Line 10 is the last of the script .
asked by Orici 23.04.2017 в 20:58
source

1 answer

3

The syntax HEREDOC set for the tag Closing that:

  • It has to be at the beginning of the line.
  • Nothing more than the closing tag on that line is allowed.


That is, that comment can not be on the same line as the closing:

$heredoc = <<<XXX
    ...
XXX; // Este comentario genera que no se cierre (ERROR) <----

Fixed:

<?php

$html = <<<XXX
            <script type="text/javascript">
            ...
            </script>
XXX;
//must begin in column 0 or ERR

echo $html;
    
answered by 23.04.2017 / 21:23
source