【问题标题】:Why do these Heredoc and Nowdoc cause errors?为什么这些 Heredoc 和 Nowdoc 会导致错误?
【发布时间】:2013-05-20 16:27:23
【问题描述】:

我已经找到了一些解决方案,但不知道发生了什么...

示例 1:

<?php

echo <<< EOD
test
EOD;

示例 2:

<?php

echo <<< 'EOD'
test
EOD;

输出 1,2:

PHP 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)

示例 3:

<?php

echo <<< EOD
test
EOD;

?>

示例 4:

<?php

echo <<< 'EOD'
test
EOD;

?>

示例 5:

<?php

echo <<< EOD
test
EOD;
'dummy';

示例 6:

<?php

echo <<< 'EOD'
test
EOD;
'dummy';

输出 3、4、5、6:

test

【问题讨论】:

    标签: php heredoc nowdoc


    【解决方案1】:

    前两个示例中的终止符后可能有空格,例如

    EOD;[space]
    

    有了这个:

    <?php
    echo <<<EOL
    test
    EOL;[space]
    

    我收到您的错误消息,但没有空格,没有错误。不管有没有结束?&gt;,这都是真的。

    【讨论】:

    【解决方案2】:

    结束分隔符必须从第一列开始,在其前面不得有空格或制表符。请注意,5.5 和 5.6 中的情况正在发生变化

    'EOD' 等同于echo ' '
    "EOD" 等同于echo " " 关于变量替换。

    结束分隔符不带任何单引号。

    【讨论】:

      【解决方案3】:

      Heredoc 非常挑剔。确保在您的初始 EOD 之后没有空格。 最后的 EOD;必须在它自己的行上,前面没有空格,并且该行上没有其他内容。正是这些空间给大多数人带来了麻烦。

      “EOD”周围的引号不是必需的。

      【讨论】:

      【解决方案4】:

      我花了一些时间来掌握heredoc 和nowdoc。 Heredoc 是一项非常棘手的业务。这是诀窍,终止符应始终位于单行上,除了开始终止符,您可以在其中添加变量或回显字符串,不要忘记它也不允许缩进结束标记

      <?php
      
      $string = <<<EXP // There should be nothing following this terminator <<<EXP
          You string goes here and it can include commas, quotes etc
      EXP;// This should be on a single line without anything else. Hit enter to make it     contain      this line, even if there is nothing to write, still hit enter, because it     should be the master of this line
      
      $string = <<<EXP2
      I am a developer.
      EXP2// Hit enter, please always do
      
      echo $string // Works perfect
      
      ?>
      

      【讨论】:

      • "Always Hit enter after the terminator" 是诀窍,否则我们会看到"Parse error: syntax error, unexpected end of file"
      【解决方案5】:

      还有一件事:结束标签不是“随便你给什么”,它必须是一个有效的标识符,只有字母数字(下划线很好),如果你给它任何其他字符,你会非常失望。

      看:

      $str = <<<'HEY WHAT THE HECK'
      
          This will not work
          and doesn't help you understand why
      
      HEY WHAT THE HECK
      ;
      

      PHP 会给你一个语法错误。 现在,如果您删除空格,

      $str = <<<'HEYWHATTHEHECK'
      
          Suddenly it's all peaches & cream
      
      HEYWHATTHEHECK
      ;
      

      (我只是尝试使用一些 UUID 来确保它不会发生在字符串中。经过 25 分钟的咒骂,我终于意识到了这一点。)

      现在对接快乐!

      【讨论】:

        【解决方案6】:

        我得到一个错误

        ( ! ) 解析错误:语法错误,文件意外结束,预期变量 (T_VARIABLE) 或 heredoc 结束 (T_END_HEREDOC) 或 ${ (T_DOLLAR_OPEN_CURLY_BRACES) 或 {$ (T_CURLY_OPEN) in ....php 第 51 行

        这发生在我在 EOD 结束后没有换行符时;

        
            echo <<<EOD
        <!DOCTYPE html>
        <html>
        <header>
        
        </header>
        <body>
        
        <form method="post" enctype="multipart/form-data">
            Select image to upload:
            <input type="file" accept="text/csv" name="fileToUpload" id="fileToUpload" >
            <input type="submit" value="Upload CSV" name="submit">
        </form>
        
        </body>
        </html>
        EOD;
        

        如果在 EOD 结束文件;然后它出错,添加一个换行符/返回。

        php 7.2.28

        【讨论】:

          猜你喜欢
          • 2012-05-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-10-11
          • 2011-03-16
          • 2023-04-10
          • 1970-01-01
          相关资源
          最近更新 更多