Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP heredoc syntax

Tags:

php

heredoc

I was wondering whether:

$foo = <<< EOT
Hello, World!
EOT;

is just as valid as

$foo = <<<EOT
Hello, World!
EOT;

and in particular whether this is true in all versions of PHP (or just the latest ones).

I wonder because I want to know whether a space between the <<< and first EOT identifier is syntactically valid. For instance, my PHP interpreter 5.3.10 runs this correctly but my vim text editor does not syntax-highlight the heredoc in the same way if there is a space between <<< and EOT (the EOT identifier is colored white instead of purple).

So what is the deal here? Are both legal in all versions of PHP or not?

like image 772
John Goche Avatar asked Feb 19 '23 21:02

John Goche


2 Answers

Tabs and spaces are allowed, and apparently so are quotes:

<ST_IN_SCRIPTING>b?"<<<"{TABS_AND_SPACES}({LABEL}|([']{LABEL}['])|(["]{LABEL}["])){NEWLINE} {

Source

Edit:

  • tabs and spaces are allowed from at least 2001
  • quotes were added in 2008
like image 187
Sjoerd Avatar answered Feb 22 '23 09:02

Sjoerd


The manual says (emphasis mine) that

A third way to delimit strings is the heredoc syntax: <<<. After this operator, an identifier is provided, then a newline.

To me this means that the space is optional (and will always be optional), since in the language as a whole identifiers can be separated from neighboring tokens by any amount of whitespace -- including none.

like image 27
Jon Avatar answered Feb 22 '23 09:02

Jon