4

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?

4
  • I would always trust the compiler/interpretor before i trust a syntax higlighting parser. If it works it works. Commented Jul 13, 2012 at 9:27
  • 1
    if it works, the only thing he knows is that it works with that version. Commented Jul 13, 2012 at 9:28
  • Whether it works with one version or all versions, you can't rely on it working in the future unless PHP specifically allows it. EDIT - which apparently it does... according to answers below. Commented Jul 13, 2012 at 9:31
  • 1
    Welcome to Stack Overflow! Please learn how to accept answers ... if you have no idea what I'm talking about click here Commented Jul 13, 2012 at 9:31

3 Answers 3

5

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
Sign up to request clarification or add additional context in comments.

1 Comment

Even though this is how the code looks right now, the manual states you should only use alphanumeric chars and underscores, so this code could change in future PHP versions.
3

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.

Comments

0

No, you should not provide a space between the <<< and the identifier. As specified in the PHP documentation:

(...) the identifier must follow the same naming rules as any other label in PHP: it must contain only alphanumeric characters and underscores, and must start with a non-digit character or underscore.

Source: http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

2 Comments

The space is invalid, because the identifier should be there right after the >>>; as quoted: After this operator, an identifier is provided, then a newline
That's exactly what it looks like to the parser even if you include a hundred spaces in between.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.