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

2

8

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;
Loydloydie answered 4/4, 2019 at 19:14 Comment(2)
This might be better off as an answer here: #12770482Legitimize
@Legitimize didn't know about that, thanks. My only thought is that a lot of people may specifically be seeing this when upgrading to PHP 7.3 because of the syntax change. So it's very version specificLoydloydie
L
14

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:

  • The closing marker is indented further than 1 or more lines within the body

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

  • I've chosen a marker HTML which is also present within the string. Because of the flexible spacing now allowed, PHP was incorrectly detecting that the string had closed before I intended.
Loydloydie answered 4/4, 2019 at 19:14 Comment(0)
C
2

Had similar issue the indentation of the closing "output;" was an issue. Leave no space on the left.

    $html = <<<HTML
      <html>
       <body>
         HTML test
       </body>
      </html>
    OUTPUT;        // leave no space on the left before the code
    echo $html;
    }
Citreous answered 19/2, 2022 at 10:8 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.