Why do these Heredoc and Nowdoc cause errors?

mpyw picture mpyw · May 25, 2013 · Viewed 11.6k times · Source

I've already found some solutions, but can't know what happened...

Example 1:

<?php

echo <<< EOD
test
EOD;

Example 2:

<?php

echo <<< 'EOD'
test
EOD;

Output 1,2:

PHP Parse error:  syntax error, unexpected end of file, expecting variable (T_VARIABLE) or heredoc end (T_END_HEREDOC) or ${ (T_DOLLAR_OPEN_CURLY_BRACES) or {$ (T_CURLY_OPEN)

Example 3:

<?php

echo <<< EOD
test
EOD;

?>

Example 4:

<?php

echo <<< 'EOD'
test
EOD;

?>

Example 5:

<?php

echo <<< EOD
test
EOD;
'dummy';

Example 6:

<?php

echo <<< 'EOD'
test
EOD;
'dummy';

Output 3,4,5,6:

test

Answer

Marc B picture Marc B · May 25, 2013

You probably have spaces after the terminator in your first two examples, e.g.

EOD;[space]

With this:

<?php
echo <<<EOL
test
EOL;[space]

I get your error message, but WITHOUT the space, there's no error. And that's true whether there's a closing ?> or not.