Skip to content

Commit f3ce63b

Browse files
authored
Add section describing nowdoc and heredoc
1 parent 8201676 commit f3ce63b

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

spec.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,6 +1076,72 @@ $instance = new class extends \Foo implements
10761076
};
10771077
~~~
10781078

1079+
## 9. Heredoc and Nowdoc
1080+
1081+
A nowdoc SHOULD be used wherever possible. Heredoc MAY be used when a nowdoc
1082+
does not satisfy requirements.
1083+
1084+
Heredoc and nowdoc syntax is largely governed by PHP requirements with the only
1085+
allowed variation being indentation. Declared heredocs or nowdocs MUST
1086+
begin on the same line as the context the declaration is being used in.
1087+
Subsequent lines in the heredoc or nowdoc MUST be indented once past the scope
1088+
indentation they are declared in.
1089+
1090+
The following is ***not allowed*** due to the heredoc beginning on a
1091+
different line than the context it's being declared in:
1092+
```php
1093+
$notAllowed =
1094+
<<<'COUNTEREXAMPLE'
1095+
This
1096+
is
1097+
not
1098+
allowed.
1099+
COUNTEREXAMPLE;
1100+
```
1101+
1102+
Instead the heredoc MUST be declared on the same line as the variable
1103+
declaration it's being set against.
1104+
1105+
The follow is ***not allowed*** due to the scope indention not matching the scope the
1106+
heredoc is declared in:
1107+
```php
1108+
function notAllowed()
1109+
{
1110+
$notAllowed = <<<'COUNTEREXAMPLE'
1111+
This
1112+
is
1113+
not
1114+
allowed.
1115+
COUNTEREXAMPLE
1116+
}
1117+
```
1118+
1119+
Instead, the heredoc MUST be indented once past the indentation of the scope
1120+
it's declared in.
1121+
1122+
The following is an example of both a heredoc and a nowdoc declared in a
1123+
compliant way:
1124+
```php
1125+
function allowed()
1126+
{
1127+
$allowed = <<<COMPLIANT
1128+
This
1129+
is
1130+
a
1131+
compliant
1132+
heredoc
1133+
COMPLIANT;
1134+
1135+
$allowedNowdoc = <<<'COMPLIANT'
1136+
This
1137+
is
1138+
a
1139+
compliant
1140+
heredoc
1141+
COMPLIANT;
1142+
}
1143+
```
1144+
10791145
[PSR-1]: https://www.php-fig.org/psr/psr-1/
10801146
[PSR-2]: https://www.php-fig.org/psr/psr-2/
10811147
[keywords]: http://php.net/manual/en/reserved.keywords.php

0 commit comments

Comments
 (0)