7

Can someone here explore the intricacies of using heredoc in PHP using example PHP code snippets, based on what the manual says below?

The php.net manual says:

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 \n 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 properties. Since PHP 5.3, this limitation is valid only for heredocs containing variables.

Here is the link to PHP manual: http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

3
  • What intricacies do you mean?
    – Pekka
    Commented Sep 28, 2011 at 9:53
  • Automatically indenting the a heredoc string that is part of class method. Some IDE, like Zend Studio allows you to select code and automatically indent it for you. That will indent the heredoc string. Is that right? Did you try that? I did not yet because it is going to be time consuming to me. I am just a php beginner. But is is one of the questions in my mind. Commented Sep 28, 2011 at 10:38

4 Answers 4

8

This is a simple use:

$bar = "stackoverflowpro";
$foo = <<<HTML
<p>Hello $bar</p>
HTML;
3
  • What if it is part of a class method? You will need to indent that for code readability. right? How are you going to format the php class code that includes your code above? Commented Sep 28, 2011 at 10:02
  • This code can be used both within a simple php page or inside a method. Example of use in a method might be to return the variable $foo and then print it wherever you want!
    – JellyBelly
    Commented Sep 28, 2011 at 10:53
  • the code inside a heredoc unfortunately can not be formatted!
    – JellyBelly
    Commented Sep 28, 2011 at 10:54
7
echo <<< _HTML

... some HTML code here ...

_HTML;

Important is that the closing tag is precisely the same as defined after the "<<<" and that the closing tag is placed without indent (no tabs, completely sticky to the left) and is closed by a ";".

Note: the "_" is not required, but I use it to indicate better that it is a heredoc which is closing.

6
  • What if it is part of a class method? You will need to indent that for code readability? right? Commented Sep 28, 2011 at 9:57
  • Sorry, I don't understand what you are trying to ask.
    – JNDPNT
    Commented Sep 28, 2011 at 9:58
  • What I meant to say is: if your code above is part of a php class and you need to format the code you probably need to indent the end delimiter line. Will that break the rule? Commented Sep 28, 2011 at 10:07
  • You can indent the html-code, but indenting the php-code for that specific closure tag is not possible. However this shouldn't be a problem for compiling the php, it's just less readable.
    – JNDPNT
    Commented Sep 28, 2011 at 10:13
  • The reason I am asking this is that Zend Studio formatter automatically indent the code for you. Have you ever tried that? Commented Sep 28, 2011 at 10:19
6

Note: a minor incompatible change was made to heredoc in PHP 7.3. See the second section for the changes since PHP 7.3.

Pre PHP 7.3

The closing heredoc identifier MUST NOT have any characters at all between the start of line and the identifier, it MAY have a single ; IMMEDIATELY afterwards and MUST NOT have any other characters after it. If the identifier gets indented it must be treated as part of the heredoc string. The only character that may appear before the newline is ;. You can't even include any whitespace between the identifier and the ; or between the ; and the newline. This means that if you use a heredoc inside a function call, you must insert a line break just after the closing identifier (i.e. before any , or ), etc.).

In other words, the only thing that can appear on the line with the closing identifier is the identifier itself and optionally one semicolon (;) immediately after the identifier. The next character (if not at End-Of-File) MUST be a valid newline character for the operating system that PHP is running on.

This is a valid heredoc string:

$text = <<<EOT
Hello!
EOT;

This heredoc string hasn't been closed and   EOT; is considered part of the string:

  $text = <<<EOT
  Hello!
  EOT;

The previous example fixed:

  $text = <<<EOT
  Hello!
EOT;

A heredoc inside a function call (note that ); must appear on a new line to work):

print(<<<EOT
Hello!
EOT
);

The same as above with very weird indentation (note that the only thing on the closing identifier line is the identifier and a newline). Hello! will have five spaces before it in the string:

           print(<<<EOT
     Hello!
EOT
                     );

Extra rules post PHP 7.3

In PHP 7.3 and later, the identifier is allowed to have whitespace before it, as long as it matches the whitespace indentation in the rest of the string. Spaces and tabs can't be mixed and the indentation of every line in the string must be identical. Extra horizontal whitespace of any type can appear immediately after the indentation whitespace except immediately before the closing identifier.

This previous example is now valid in PHP 7.3 and above:

  $text = <<<EOT
  Hello!
  EOT;

The identifier can no longer appear anywhere else within the string, except within a longer identifier or after printing (non-whitespace) characters. Identifiers consist of letters and underscores, but not numbers or symbols.

Invalid (+ is not a letter or underscore):

$text = <<<FOO
  Hello!
    FOO+
  FOO;

Valid (Hello!\n FOOD):

$text = <<<FOO
  Hello!
    FOOD
  FOO;

Valid (Hello!\n A FOO):

$text = <<<FOO
  Hello!
    A FOO
  FOO;

The closing identifier no longer needs to be the only thing on its line (except for the previously mentioned optional semicolon and indentation).

Valid (Hello!1588648007):

$text = <<<FOO
  Hello!
  FOO . time();
0
0

Just to refresh the discussion. Since PHP ver. 7.3 and upper there are some new rules in HEREDOC syntax:

The closing marker for doc strings is no longer required to be followed by a semicolon or newline. Additionally the closing marker may be indented, in which case the indentation will be stripped from all lines in the doc string.

See the docs: https://www.php.net/manual/en/migration73.new-features.php

Try it here: Online PHP tester with my example

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.