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 expression, so only a closure can be declared: hence, the request for a function, fn. That closure could be static or have another attribute.

In the end, the closure is not compatible with the + or similar operator, so the suggestion is not valid here. It could be valid with a comma.

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

1 + #[attribute];

Solutions

  • Remove the attribute.

  • Turn the # comment into a // comment.