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

throw Is An Expression

throw was a standalone expression: it needed to be alone, between semicolons (or equivalents).

Since PHP 8.0, throw may be included in another expression. This is useful with or, or the coalesce operator, to execute the expression when a value is missing or failing.

PHP code

<?php

foo() or throw new \Exception();

$x = $_GET['x'] ?? throw new \Exception('Missing value for x');

?>

Before

PHP Parse error:  syntax error, unexpected 'throw' (T_THROW)

Parse error: syntax error, unexpected 'throw' (T_THROW)

After


PHP version change

This behavior changed in 8.0.

See Also

Error Messages

Analyzer

© 2023-2026, Damien Seguy - Exakat