Heredoc不工作

时间:2010-09-17 23:41:40

标签: php heredoc

<?php

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

echo $information;

?>

结果:

  

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

4 个答案:

答案 0 :(得分:24)

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

答案 1 :(得分:4)

我刚编辑了你的问题并修复了无效的格式(SO正在使用Markdown)。我发现在<<<INFO之后有一个空格字符 - 会导致错误。

删除该空格,一切都应该正常......好吧 - it has to works fine

答案 2 :(得分:3)

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;

答案 3 :(得分:0)

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;
        ?>