简体   繁体   中英

Heredoc and closing tags issue

case 1:

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

versus

case 2:

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

I have a question about heredoc usage. I find some weird behavior for which I could use an explanation.

When I try case 1, I do not get the "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!"

This is also true when I remove the closing tag in case 2 and just add an extra line instead.

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 (;). 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.

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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM