Heredoc不工作

Ene*_*ate 13 php heredoc

<?php

$information = <<<INFO 
Name: John Smith
Address: 123 Main St
City: Springville, CA
INFO;

echo $information;

?>
Run Code Online (Sandbox Code Playgroud)

结果:

解析错误:语法错误,第3行意外的T_SL

Rob*_*ell 26

解析器正在抱怨,因为在有角度的括号声明一个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;
Run Code Online (Sandbox Code Playgroud)


Erd*_*acı 5

Heredoc 语法有一些我们必须考虑的严格规则;

1 - 打开标识符后不应有任何字符

真的

"$a = <<<HEREDOC"
Run Code Online (Sandbox Code Playgroud)

错误的

"<<<HEREDOC   "   //Remove space after opening identifier;
Run Code Online (Sandbox Code Playgroud)

;2 - 除了末尾的分隔符分号之外,结束标识符前后不应有任何其他字符。也不允许有缩进。

真的

"HEREDOC;"
Run Code Online (Sandbox Code Playgroud)

错误的

"HEREDOC  ;"   //Remove space between HEREDOC and ;
Run Code Online (Sandbox Code Playgroud)

错误的

" HEREDOC;"   //Remove space before HEREDOC
Run Code Online (Sandbox Code Playgroud)

错误的

"HEREDOC; "   //Remove space after ;
Run Code Online (Sandbox Code Playgroud)

定界符字符串。结尾;