syntax error, unexpected token “)”, expecting “function” or “fn” or “static” or “#[”

Description

In this command, an attribute has been configured. The attributes can only precede a structure, such as a class, method or function.

In this case, the attribute has been placed inside an function call, so only a closure can be declared.

This error appears after PHP 8.0: this is when the #[ token was introduced. Until that version, # introduces a comment, until the end of the line.

Example

<?php

foo(#[a]);

function foo(Closure $f) {}

?>

Solutions

  • Finish declaring a closure, with fn or function in the argument.

  • Remove the attribute.

  • Turn the attribute into a comment.