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 “?”

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) {}

?>

Alternatives

  • Finish the expression before the ? in a ternary operation.
  • Use the null type when specifying a type.