Skip to content

Commit 0e8f81a

Browse files
authored
Merge pull request #5 from KorvinSzanto/heredoc-nowdoc
Add section describing nowdoc and heredoc
2 parents b5cac84 + f3ce63b commit 0e8f81a

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
@@ -1081,6 +1081,72 @@ $instance = new class extends \Foo implements
10811081
};
10821082
```
10831083

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

0 commit comments

Comments
 (0)