strict_types declaration must be the very first statement in the script

Description

This error appears when the declare statement is not the first statement in the file.

It appears even when non-executing statement are placed before it, such as forgotten characters before the opening tag, any kind of operations in PHP commands, including definitions, namespaces, etc.

Other declare() calls may appears before the strict_types one, though. Also, PHP uses this error with the other declare statement, such as ticks or encoding. They are very rare though.

Example

Here<?php

$a = 1;
declare(strict_types = 1);

?>

Solutions

  • Check if any characters are left before the opening PHP tag.

  • Move the declare after the PHP opening tag.