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

当我运行它时报告:

Parse error: parse error on line 2

但我没有看到任何异常。

4

3 回答 3

6

我相信问题是你应该使用$output = <<<END而不是$output = <<< END(注意空间不足)

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

确保之前或之后没有空格,END即使是一个无关的空格也会导致问题。查看您发布的代码,您似乎在END.

php 网站

警告

需要注意的是,带有结束标识符的行不能包含其他字符,可能除了分号 (;)。这尤其意味着标识符可能不会缩进,并且分号之前或之后可能没有任何空格或制表符。同样重要的是要认识到结束标识符之前的第一个字符必须是本地操作系统定义的换行符。这是在 UNIX 系统上的 \n,包括 Mac OS X。结束分隔符(可能后跟分号)还必须后跟换行符。

如果这条规则被破坏并且关闭标识符不是“干净的”,它将不会被视为关闭标识符,PHP 将继续寻找一个。如果在当前文件结尾之前没有找到合适的结束标识符,则在最后一行会导致解析错误。

于 2010-06-10T02:58:37.587 回答
2

它应该是<<<END(没有空格)。

例子:

<?php
    echo <<<END
Heredoc string.
END;
于 2010-06-10T02:57:41.307 回答
1

在我的本地主机上使用以下内容不会输出任何错误。

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

就像其他人提到的那样,我只是摆脱了空间。

于 2010-06-10T03:08:46.787 回答