15
<?php

$information = <<<INFO 
Name: John Smith
Address: 123 Main St
City: Springville, CA
INFO;

echo $information;

?>

Result:

Parse error: syntax error, unexpected T_SL on line 3

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Enemy of the State
  • 228
  • 1
  • 2
  • 6
  • 1
    In my case, I had the entire heredoc string indented, so I wasn't following the documentation that says `The closing identifier must begin in the first column of the line.` – User Apr 29 '14 at 05:54
  • 1
    @User great comment, this was my issue. PHP WHY YOU SO BAD –  Jun 03 '15 at 00:04
  • You will also see this error if you have a different heredoc in the file with the same name but the other one has an extra space or parenthesis or something – William Entriken Oct 15 '19 at 18:26

4 Answers4

29

The parser is complaining because you have whitespace after the angled brackets declaring a heredoc. You need to make sure you're actually following the heredoc syntax, which you can find on the PHP Manual site (specifically: http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc).

<?php
$information = <<<ENDHEREDOC
this is my text
ENDHEREDOC;
echo $information;
Robert Elwell
  • 6,598
  • 1
  • 28
  • 32
5

Heredoc syntax has some strict rules we have to consider;

1 - There shouldn't be any character after opening identifier

True

"$a = <<<HEREDOC"

False

"<<<HEREDOC   "   //Remove space after opening identifier;

2 - There shouldn't be any other character after and before closing identifier except delimiter semicolon ; at the end. Also no indentation is allowed.

True

"HEREDOC;"

False

"HEREDOC  ;"   //Remove space between HEREDOC and ;

False

" HEREDOC;"   //Remove space before HEREDOC

False

"HEREDOC; "   //Remove space after ;

Heredoc string. END;

Erdinç Çorbacı
  • 1,187
  • 14
  • 17
  • Please also consider and add the requirement that the ending identifier must also not be indented after the newline - as per the heredoc documentation. That was the hang up in mine. – Shmack Jan 24 '20 at 22:31
  • I will, but indeed indentation was also meant in "2 - There shouldn't be any other character after and before closing identifier" :) – Erdinç Çorbacı Jan 27 '20 at 03:38
4

I've just edited your question and fixed invalid formatting (SO is using Markdown). I found out that there is a space character after <<<INFO - that causes the error.

Delete that space and everything should work fine... well - it has to works fine.

Crozin
  • 43,890
  • 13
  • 88
  • 135
0
https://repl.it/@CiscoTest/PHP-Heredocs-lesslessless      
    <?php
        //Heredocs start with <<< and a token ended with semi-colon
        print <<< ENDHEREOK
        We used ENDHEREOK "as" our token
              Looks like it just "print"
              things "as" it is. Let me loooook at what I just typed

        I may add some more! I m gonna end it using ENDHEREOK but any token can be used
        Give it a "try"! Also pay attention to so many double quotes because it is mandatory!
         Also yes "if" you put
        space after token(ENDHEREOK) above, you will get an error, just hit enter key after token!
        Try this on repl.it
        https://repl.it/@CiscoTest/PHP-Heredocs-lesslessless
        ENDHEREOK;
        ?>
Cisco Test
  • 41
  • 2