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

null cannot be marked as nullable

Description

Since PHP 8.2, null (like false and true) can be used directly as a standalone type. But null already means exactly one thing: the value null. Marking it nullable with a leading ?, as in ?null, would only mean null or null, which is entirely redundant, so PHP rejects it.

Example

<?php

function test(): ?null
{
}

?>

Literal Examples

  • null cannot be marked as nullable

Alternatives

  • Remove the ? and use null on its own.

Changed Behavior

This error may appear following an evolution in behavior, in previous versions. See