简体   繁体   English

PHP heredoc语法

[英]PHP heredoc syntax

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). 特别是在所有版本的PHP(或者只是最新的版本)中是否都是这样。

I wonder because I want to know whether a space between the <<< and first EOT identifier is syntactically valid. 我想知道因为我想知道<<<和第一个EOT标识符之间的空格是否在语法上有效。 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). 例如,我的PHP解释器5.3.10正确运行,但是如果<<<EOT之间有空格( EOT标识符是白色而不是紫色),我的vim文本编辑器不会以相同的方式突出显示heredoc。 。

So what is the deal here? 那么这里的交易是什么? Are both legal in all versions of PHP or not? 在所有版本的PHP中是否合法?

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 至少2001年允许使用制表符和空格
  • quotes were added in 2008 报价于2008年增加

The manual says (emphasis mine) that 手册说(强调我的)

A third way to delimit strings is the heredoc syntax: <<<. 分隔字符串的第三种方法是heredoc语法:<<<。 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. 对我来说,这意味着空间是可选的(并且将始终是可选的),因为在整个语言中,标识符可以通过任何数量的空格与相邻标记分开 - 包括无空格。

No, you should not provide a space between the <<< and the identifier. 不,你不应该在<<<和标识符之间提供一个空格。 As specified in the PHP documentation: 如PHP文档中所指定:

(...) 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 . (...)标识符必须遵循与PHP中任何其他标签相同的命名规则:它必须仅包含字母数字字符和下划线 ,并且必须以非数字字符或下划线开头

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM