PHP: rfc:flexible_heredoc_nowdoc_syntaxes
source link: https://wiki.php.net/rfc/flexible_heredoc_nowdoc_syntaxes
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Closing Marker Indentation
The indentation of the closing marker will change code from:
<?php class foo { public $bar = <<<EOT bar EOT; }
<?php class foo { public $bar = <<<EOT bar EOT; }
The indentation of the closing marker dictates the amount of whitespace to strip from each line within the heredoc/nowdoc. So let's demonstrate these semantics with a few examples:
// no indentation echo <<<END a b c END; /* a b c */ // 4 spaces of indentation echo <<<END a b c END; /* a b c */
If the closing marker is indented further than any lines of the body, then a ParseError
will be thrown:
echo <<<END a b c END; // Parse error: Invalid body indentation level (expecting an indentation at least 5) in %s on line %d
Tabs are supported as well, however, tabs and spaces must not be intermixed regarding the indentation of the closing marker and the indentation of the body (up to the closing marker). In any of these cases, a ParseError
will be thrown:
// different indentation for body (spaces) ending marker (tabs) { echo <<<END a END; } // mixing spaces and tabs in body { echo <<<END a END; } // mixing spaces and tabs in ending marker { echo <<<END a END; }
These whitespace constraints have been included because mixing tabs and spaces for indentation is harmful to legibility.
Ultimately, the purpose of stripping leading whitespace is to allow for the body of the heredoc and nowdoc to be indented to the same level as the surrounding code, without causing unnecessary (and perhaps undesirable) whitespace to prepend each line of the body text. Without this, developers may choose to de-indent the body text to prevent leading whitespace, which leads us back to the current situation of having indentation levels of code ruined by these syntaxes.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK