syntax error, unexpected token “)”, expecting variable

Description

An attribute placed inside a parameter list must be immediately followed by the parameter it decorates. Here, the attribute is the last thing before the closing parenthesis, so PHP expects a variable to declare the parameter, but finds the closing parenthesis instead.

Example

<?php

function foo(string $a, #[SomeAttribute]) {}

?>

Solutions

  • Add the missing parameter, with its $ variable, right after the attribute.

  • Remove the trailing attribute if no extra parameter was intended.

See Also

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>`_.