<?php
$information = <<<INFO
Name: John Smith
Address: 123 Main St
City: Springville, CA
INFO;
echo $information;
?>
结果:
解析错误:第3行出现意外的T_SL符号
<?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字符串。
END;
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;
?>