syntax error, unexpected token “?”

Description

? is used in several syntaxes in PHP.

  • as null in a type specification (argument, returntype, property, class constant), when it is the first type specified: ?A $a

  • as the terny operator `` $condition ? $then : $else ``. Then, the condition must be a complete expression.

Example

<?php

// an unfinished ternary operation
$a = 1 + ? 3 : 2 ;

// ? should be written null in a type specification
function foo(a|? $a) {}

?>

Solutions

  • Finish the expression before the ? in a ternary operation.

  • Use the null type when specifying a type.