Skip to content

Fix GH-15796: Add the exit_status() function #19445

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ PHP NEWS
opened directory has been deprecated. (Girgias)
. Fixed bug GH-19153 (#[\Attribute] validation should error on
trait/interface/enum/abstract class). (DanielEScherzer)
. Added the exit_status() function to retrieve the current exit
status. (alexandre-daubois)

31 Jul 2025, PHP 8.5.0alpha4

Expand Down
1 change: 1 addition & 0 deletions UPGRADING
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,7 @@ PHP 8.5 UPGRADE NOTES
- Standard:
. Added array_first() and array_last().
RFC: https://wiki.php.net/rfc/array_first_last
. Added exit_status().

========================================
7. New Classes and Interfaces
Expand Down
17 changes: 17 additions & 0 deletions Zend/tests/exit_status/exit_status_exception.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
--TEST--
exit_status() with uncaught exceptions setting exit code
--FILE--
<?php
register_shutdown_function(function() {
echo "Exit status is: " . exit_status() . "\n";
});

// set exit status to 255
throw new Exception("Test exception");
?>
--EXPECTF--
Fatal error: Uncaught Exception: Test exception in %s:%d
Stack trace:
#0 {main}
thrown in %s on line %d
Exit status is: 255
20 changes: 20 additions & 0 deletions Zend/tests/exit_status/exit_status_shutdown.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
--TEST--
exit_status() in shutdown handler preserving original exit code
--FILE--
<?php
register_shutdown_function(function() {
echo "Current exit status: " . exit_status() . "\n";

$original_status = exit_status();
$final_status = $original_status ?: 255;

echo "Final status will be: " . $final_status . "\n";
});

echo "Setting exit status to 42\n";
exit(42);
?>
--EXPECT--
Setting exit status to 42
Current exit status: 42
Final status will be: 42
14 changes: 14 additions & 0 deletions Zend/tests/exit_status/exit_status_string.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
exit_status() with string exit codes
--FILE--
<?php
register_shutdown_function(function() {
echo "Exit status: " . exit_status() . "\n";
});

echo "Before exit with string\n";
exit("Error message");
?>
--EXPECT--
Before exit with string
Error messageExit status: 0
7 changes: 7 additions & 0 deletions Zend/zend_builtin_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,13 @@ ZEND_FUNCTION(error_reporting)
}
/* }}} */

ZEND_FUNCTION(exit_status)
{
ZEND_PARSE_PARAMETERS_NONE();

RETURN_LONG(EG(exit_status));
}

static bool validate_constant_array_argument(HashTable *ht, int argument_number) /* {{{ */
{
bool ret = 1;
Expand Down
2 changes: 2 additions & 0 deletions Zend/zend_builtin_functions.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ function strncasecmp(string $string1, string $string2, int $length): int {}

function error_reporting(?int $error_level = null): int {}

function exit_status(): int {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
function exit_status(): int {}
function exit_status(): ?int {}

Would make more sense to indicate we are in shutdown process or not.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure to get your comment. Where/when should this function return null?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When exit was not called yet.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When exit is not called yet, the return code of the process will be 0. Also, it can be changed to 255 when an uncaught exception occurs. Thus, I'm not sure this function should return null, because the process will always have an exit code, no matter what.


function define(string $constant_name, mixed $value, bool $case_insensitive = false): bool {}

function defined(string $constant_name): bool {}
Expand Down
6 changes: 5 additions & 1 deletion Zend/zend_builtin_functions_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.