简体   繁体   English

Heredoc和结束标签问题

[英]Heredoc and closing tags issue

case 1: 情况1:

<?php    
echo <<<"FOOBAR"
Hello World!
FOOBAR;

versus

case 2: 情况2:

<?php
echo <<<"FOOBAR"
Hello World!
FOOBAR;
?>

I have a question about heredoc usage. 我对heredoc的使用有疑问。 I find some weird behavior for which I could use an explanation. 我发现一些奇怪的行为,可以对此做出解释。

When I try case 1, I do not get the "Hello World!" 当我尝试案例1时,我没有收到“ Hello World!” output printed. 输出打印。 Instead I see the following parse error. 相反,我看到以下解析错误。

Parse error: syntax error, unexpected end of file, expecting variable (T_VARIABLE) or heredoc end (T_END_HEREDOC) or ${ (T_DOLLAR_OPEN_CURLY_BRACES) or {$ (T_CURLY_OPEN) in C:\xampp\htdocs\test\index.php on line 5

However, in case 2, I see it print correctly the text "Hello World!" 但是,在第2种情况下,我看到它正确打印了文本“ Hello World!”。

This is also true when I remove the closing tag in case 2 and just add an extra line instead. 当我删除第2种情况下的结束标记并添加一条额外的行时,也是如此。

Can anybody explain the behavior and why it happens? 任何人都可以解释该行为及其原因吗?

You are right 你是对的

check Explanation:- It is very important to note that the line with the closing identifier must contain no other characters, except a semicolon (;). check说明:-请务必注意,带有结束标识符的行除分号(;)外,不得包含其他任何字符。 That means especially that the identifier may not be indented, and there may not be any spaces or tabs before or after the semicolon. 特别是这意味着标识符可能不会缩进,并且在分号之前或之后可能没有任何空格或制表符。

It's also important to realize that the first character before the closing identifier must be a newline as defined by the local operating system. 同样重要的是要认识到,结束标识符之前的第一个字符必须是本地操作系统定义的换行符。 This is \\n on UNIX systems, including Mac OS X. The closing delimiter must also be followed by a newline. 在包括Mac OS X在内的UNIX系统上,这是\\ n。结束定界符也必须后跟换行符。

If this rule is broken and the closing identifier is not "clean", it will not be considered a closing identifier, and PHP will continue looking for one. 如果该规则被破坏,并且结束标识符不是“干净的”,它将不会被视为结束标识符,PHP将继续寻找一个。 If a proper closing identifier is not found before the end of the current file, a parse error will result at the last line . 如果在当前文件末尾之前找不到合适的结束标识符,则将在最后一行产生解析错误

for more check 更多检查
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