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
The heredoc syntax is a way to declare a string variable. The heredoc syntax takes at least three lines of your code and uses the special character <<< at the beginning.
In computing, a here document (here-document, here-text, heredoc, hereis, here-string or here-script) is a file literal or input stream literal: it is a section of a source code file that is treated as if it were a separate file.
Heredocs and nowdocs are the easiest and cleanest ways in php to use and format multiline strings. The only difference between a heredoc and a nowdoc is that a heredoc performs string interpolation, turning your variables into the string they represent, while a nowdoc does not.
Heredoc's are equivalent to a double quoted string. That means any variables in the string will get substitued for their respective values. We could rewrite the double quoted string example above as a heredoc:- $foo = 'bar'; echo <<<EOT Hello $foo Goodbye! EOT; // Output:- // Hello bar // Goodbye!
This is a simple use:
$bar = "stackoverflowpro";
$foo = <<<HTML
<p>Hello $bar</p>
HTML;
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.
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
);
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With