The exit and die are Considered Functions in PHP 8.4

The exit and die are Considered Functions in PHP 8.4

The exit and its alias die terminates the current PHP script with an exit code or message. It commonly used in the command line applications. PHP 8.4 brings changes to the exit and die behavior.

Since PHP 8.4, exit and die are functions instead of language constructs. For example, exit and die can now be called with named argument, just like regular functions:

<?php

exit(status: 1);

The exit/die can now be passed to functions as a callable or used as standard PHP callable:

<?php

$callable = 'exit';
$callable(1);

The exit/die now respect the strict_types declaration. When strict_types=1 is declared in the script, passing any value other than string or int causes a TypeError exception:

<?php

declare(strict_types=1);

exit(true);
Fatal error: Uncaught TypeError: exit(): Argument #1 ($status) must be of type string|int ...

To ensure backward compatibility with older PHP applications, exit/die can be called without the parentheses as previously.

Leave a Comment

Cancel reply

Your email address will not be published.