The php.net RFC channel has announced the Heredoc and Nowdoc syntax updates for PHP 7.3. This update focuses on code readability:
## From the current implementation of PHP 7.2, like this simple example:Heredoc and Nowdoc has a very strict syntax, which sometimes makes many developers avoid it because they look very ugly in the code, making the code less readable. In response to this problem, this update has made the following two changes to the syntax:
supports the indentation of closing tags;
Line breaks for closing tags are no longer forced;
<?php class foo { public $bar = <<<EOT bar EOT; }in version 7.3 , the following forms are available:
<?php class foo { public $bar = <<<EOT bar EOT; }The indentation of the closing tag determines the number of spaces per new line in heredoc/nowdoc:
<?php // 4 个缩进空格 echo <<<END a b c END; /* a b c */In the current implementation of PHP 7.2, A newline must exist to end heredoc/nowdoc. PHP 7.3 removes this constraint:
<?php stringManipulator(<<<END a b c END); $values = [<<<END a b c END, 'd e f'];Background of Heredoc and NowdocNowdoc is supported from PHP 5.3.0 version. The only difference between it and Heredoc is double quotes and single quotes. The difference between quotation marks. Nowdoc adds single quotes around the start tag, and there is no parsing:
<?php $name = 'Example'; $str = <<<'EOD' Example of string $name spanning multiple lines using nowdoc syntax. EOD;The above nowdoc will output:
Example of string $name spanning multiple lines using nowdoc syntax.Here The definition of the document on the wiki:
In computer science, a here document, also known as a heredoc, hereis, here-string or here-script, is a file input or data stream input: a block of code that can be treated as a complete file. It can save whitespace characters such as line breaks or indents in the text. Some languages allow variable substitution and command substitution within strings.Improvements in Heredocs and Nowdocs will make your PHP code more readable and error-prone. On the other hand, the output is more concise and direct because the indentation that would close the markup is removed. Get more information It is recommended to read the official change document - flexible Heredoc and Nowdoc Syntaxes RFC. PHP official documentation Heredoc and Nowdoc.