PHP syntax error T_ENCAPSED_AND_WHITESPACE

别说谁变了你拦得住时间么 提交于 2019-12-02 01:59:30

问题


i'm starting to work with php basics and i have some problem understanding how mix code with strings.

I found a great and useful style to print string blocks but i don't know the name and i'm not able to find examples.

the code below return me the error:
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /web/htdocs/food/user/index.php on line 120

<?php   
$html_str = <<<STR
    <li><img alt="hello" src="$path_images/pencil.png"/><a title="hello" href="$path_pages/$page/action">Details</a></li>

STR;
print $html_str;
?>

can someone help me to find where i'm wrong and the name of this syntax style?

thanks v


回答1:


I've found the problem!
in the example I've posted it can't return the error:

Working code

<?php
$str = <<<STRING
hello! this is a working string<br/>
and i can do too many things with heredoc syntax!
STRING;

print $str;
?>

Not working code

<?php
     $str = <<<STRING
     syntax error!<br/>
     syntax error!<br/>
     why?
     STRING;

     print $str;
?>

The problem are the tabs before the close tag STRING; which are considered part of tag, so the close tag is not interpreted "STRING;" but "        STRING;", that's why it doesn't work.

hope it come usefull for someone else.




回答2:


The name of the syntax is HEREDOC strings or "here documents".

But when I run your code on my server, I don't get the token errors that you do, though. Maybe your error is actually somewhere else?



来源:https://stackoverflow.com/questions/1708641/php-syntax-error-t-encapsed-and-whitespace

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!