Have trouble when using heredoc syntax in PHP [closed]

ε祈祈猫儿з 提交于 2019-12-02 19:19:14

问题


<?php
        $output = <<< END 
        <table style="display: table;" class="listview rowstyle-rowhighlight" id="resourcegrid">
          <thead>
            <tr>
              <th width="70"></th>
              <th style="-moz-user-select: none;" class="sortable fd-column-0"><a class="fdTableSortTrigger" href="#">Name</a></th>
              <th style="-moz-user-select: none;" class="sortable fd-column-1"><a class="fdTableSortTrigger" href="#">Contributor</a></th>
              <th style="-moz-user-select: none;" class="sortable fd-column-3"><a class="fdTableSortTrigger" href="#">Modified</a></th>
            </tr>
          </thead><tbody>
END;

echo $output;

When I run it reports :

Parse error: parse error on line 2

But I don't see anything abnormal.


回答1:


I believe the problem is that you should use $output = <<<END instead of $output = <<< END (Note the lack of space)

echo <<<END
heredoc string...
END;

Make sure that there is no space before or after the END even a single extraneous space could cause problems. Looking at the code you posted, you seem to have a space before and after the END.

From the php website:

Warning

It is very important to note that the line with the closing identifier must contain no other characters, except possibly 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 (possibly followed by a semicolon) 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.




回答2:


It should be <<<END (without space).

Example:

<?php
    echo <<<END
Heredoc string.
END;



回答3:


Using the following on my localhost outputs no errors.

<?php
        $output = <<<END
        <table style="display: table;" class="listview rowstyle-rowhighlight" id="resourcegrid">
          <thead>
            <tr>
              <th width="70"></th>
              <th style="-moz-user-select: none;" class="sortable fd-column-0"><a class="fdTableSortTrigger" href="#">Name</a></th>
              <th style="-moz-user-select: none;" class="sortable fd-column-1"><a class="fdTableSortTrigger" href="#">Contributor</a></th>
              <th style="-moz-user-select: none;" class="sortable fd-column-3"><a class="fdTableSortTrigger" href="#">Modified</a></th>
            </tr>
          </thead><tbody>
END;

Like others mentioned, I simply got rid of the space.



来源:https://stackoverflow.com/questions/3011349/have-trouble-when-using-heredoc-syntax-in-php

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