syntax error, unexpected token “::”, expecting “]”

Description

Inside the square brackets of an array index, PHP expects an expression identifying the key, followed by the closing ]. The scope resolution operator :: cannot start an expression on its own: it must be preceded by a class name, a variable or one of self, parent or static. Here, PHP found :: immediately, so it reports that it was still expecting ] to close the array access.

The same message also appears whenever a previous [ was left unclosed, and a later, unrelated :: is the first token that cannot continue the pending index expression.

Example

<?php

    $arr = [1, 2, 3];
    $y = $arr[::];

?>

Solutions

  • Provide the missing class name (or self, parent, static) before ::, such as $arr[X::CONST].

  • Check that every previous [ in the file has a matching ].

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