syntax error, unexpected token “use”

Description

use expression must be place at the top level of a file, a namespace block, or a class, enum or trait block.

In particular, when set in a structure such as a foreach, it yields a syntax error.

Besides that, the command is actually valid.

Example

<?php
if ($a) {
    use stdClass as A;
}

?>

Solutions

  • Move the use out of the if structure.

  • Remove the use command.