syntax error, unexpected ‘;’, expecting ‘[’

Description

The & reference operator turns a value into a reference. It works with data containers, such as variables, properties or array elements. It doesn’t work on literal constants.

The error message suggestion to use extra operators such as [ leads to a distinct error: Cannot use temporary expression in write context.

Example

<?php

$a = &E_ALL;

const B = [1,2,3];
//Cannot use temporary expression in write context
$c = &B[2];

?>

Solutions

  • Store the constant in a variable, and make the reference on the variable.

  • Use constants by value, not by reference.