phpheredocphp-7.3

Invalid body indentation level (expecting an indentation level of at least 4)


I just upgraded to PHP 7.3 and I'm getting this error:

Invalid body indentation level (expecting an indentation level of at least 4)

Here is the code:

    $html = <<<HTML
<html>
<body>
    HTML test
</body>
</html>
HTML;

Solution

  • This is caused by the new flexible Heredoc syntaxes in PHP 7.3.

    In previous versions of PHP, the closing marker was not allowed to have indentation:

        $string = <<<EOF
    Hello
    EOF;
    

    As of PHP 7.3, the closing marker can be indented.

    In this example, EOF is indented by 4 spaces. The body of the string will also have 4 spaces stripped from the beginning of each line.

        $string = <<<EOF
        Hello
        EOF;
    

    If the closing marker is indented further than any lines of the body, it will throw a Parse error:

        $string = <<<EOF
      Hello
        EOF;
    

    The reason for the error message is two-fold:

    But perhaps more likely, for those upgrading to PHP 7.3: