PHP heredoc parse error

John Griffiths picture John Griffiths · Aug 12, 2009 · Viewed 9.8k times · Source

This produces output page OK

$mystring = "<<<EOT";

Replacing it with the following produces

Parse error: syntax error, unexpected $end in file.php on line 737

$mystring = <<<EOT
   This is some PHP text.
   It is completely free
   I can use "double quotes"
   and 'single quotes',
   plus $variables too, which will
   be properly converted to their values,
   you can even type EOT, as long as it
   is not alone on a line, like this:
EOT;

Any ideas as to what is causing the parser to choke?

I'm using PHP 4.4.7.

It is only on one file that this behaviour happens all others follow the PHP defined functionality.

What I am trying to recitify is what could be possibly wrong in the proceding lines so that the PHP parser shows in this failure.

John

changed file contents to :-

<?php

$mystring = <<<WHATEVER
   This is some PHP text.
WHATEVER;
?>

result =

Parse error: syntax error, unexpected $end in file.php on line 5

Any clues

EDIT

original error was to do with T_ENCAPSED_AND_WHITESPACE this can be caused with jQuery for example "if(x == y){$('#my_image').hide():}" is within the heredoc the bigram "{$ will start the parser looking for php variable for substitution.

EDIT

2 good responses.

1) Ch4m3l3on - "<?php" vs "<?" handling.

2) The Disintegrator - <q>had a similar problem with a stupid program that insisted in putting the BOM in a utf-8 file (ignoring preferences)</q>.

EDIT

1) Replacing all content with a single block didn't fix the problem or give any other pointers.

2) No BOM (Byte Order Mark), pity as this or similar majic characters would have explained all symptoms perfectly.

Answer

Hadi picture Hadi · Apr 20, 2012

you have to place your ending heredoc at the beginning of line. if you use some IDE that have indentation, remove them! your ending heredoc must be vertically in the same line as your ending php tag(