【问题标题】:Have trouble when using heredoc syntax in PHP [closed]在 PHP 中使用 heredoc 语法时遇到问题 [关闭]
【发布时间】:2011-03-01 23:31:28
【问题描述】:
<?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

但我没有看到任何异常。

【问题讨论】:

  • END 标记实际上在第 1 列中吗?

标签: php heredoc


【解决方案1】:

应该是&lt;&lt;&lt;END(没有空格)。

例子:

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

【讨论】:

【解决方案2】:

我认为问题在于您应该使用$output = &lt;&lt;&lt;END 而不是$output = &lt;&lt;&lt; END(注意空间不足)

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

确保END 之前或之后没有空格,即使是一个无关的空格也可能导致问题。看你贴的代码,END前后好像有空格。

来自php website

警告

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

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

【讨论】:

    【解决方案3】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2013-04-23
      • 2014-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-27
      • 1970-01-01
      • 1970-01-01
      • 2012-03-17
      相关资源
      最近更新 更多