<?php
$information = <<<INFO
Name: John Smith
Address: 123 Main St
City: Springville, CA
INFO;
echo $information;
?>
结果:
解析错误:语法错误,第 3 行出现意外的 T_SL
解析器抱怨是因为在尖括号声明heredoc 之后有空格。您需要确保您实际上遵循了 heredoc 语法,您可以在 PHP 手册网站上找到该语法(特别是: http://www.php.net/manual/en/language.types.string.php#language。 types.string.syntax.heredoc)。
<?php
$information = <<<ENDHEREDOC
this is my text
ENDHEREDOC;
echo $information;
Heredoc 语法有一些我们必须考虑的严格规则;
1 - 打开标识符后不应有任何字符
真的
"$a = <<<HEREDOC"
错误的
"<<<HEREDOC " //Remove space after opening identifier;
2 - 在结束标识符之后和之前不应有任何其他字符,除了最后;
的定界符分号。也不允许缩进。
真的
"HEREDOC;"
错误的
"HEREDOC ;" //Remove space between HEREDOC and ;
错误的
" HEREDOC;" //Remove space before HEREDOC
错误的
"HEREDOC; " //Remove space after ;
Heredoc 字符串。结尾;
我刚刚编辑了您的问题并修复了无效格式(所以使用 Markdown)。我发现<<<INFO
- 之后有一个空格字符会导致错误。
删除该空间,一切都应该正常工作......好吧 -它必须正常工作。
https://repl.it/@CiscoTest/PHP-Heredocs-lesslessless
<?php
//Heredocs start with <<< and a token ended with semi-colon
print <<< ENDHEREOK
We used ENDHEREOK "as" our token
Looks like it just "print"
things "as" it is. Let me loooook at what I just typed
I may add some more! I m gonna end it using ENDHEREOK but any token can be used
Give it a "try"! Also pay attention to so many double quotes because it is mandatory!
Also yes "if" you put
space after token(ENDHEREOK) above, you will get an error, just hit enter key after token!
Try this on repl.it
https://repl.it/@CiscoTest/PHP-Heredocs-lesslessless
ENDHEREOK;
?>