Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

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;
}

?>

Alternatives

  • Move the use out of the if structure.
  • Remove the use command.