Namespace declaration statement has to be

Description

A namespace declaration must be the very first statement of the file, or immediately follow a declare() call (such as declare(strict_types=1);). Any other statement, including a simple echo, a comment that generates output, or whitespace before the opening <?php tag, placed before the namespace statement makes it invalid.

The full message reads: Namespace declaration statement has to be the very first statement or after any declare call in the script.

Example

<?php

echo 'Hello';

namespace App;

?>

Literal Examples

  • Namespace declaration statement has to be the very first statement or after any declare call in the script

Solutions

  • Move the namespace statement to be the first statement in the file, right after the opening <?php tag.

  • Only keep a declare() call before the namespace statement, and move everything else after it.

  • Remove any whitespace or output before the opening <?php tag.

Changed Behavior

This error may appear following an evolution in behavior, in previous versions. See ` <https://php-changed-behaviors.readthedocs.io/en/latest/behavior/.html>`_.