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 @@ -58,6 +58,8 @@ PHP NEWS
. The socket_set_timeout() alias function has been deprecated. (timwolla)
. Passing null to to readdir(), rewinddir(), and closedir() to use the last
opened directory has been deprecated. (Girgias)
. 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 @@ -574,6 +574,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
12 changes: 12 additions & 0 deletions Zend/tests/exit_status/exit_status_basic.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
exit_status() basic functionality test
--FILE--
<?php
var_dump(exit_status());
var_dump(function_exists('exit_status'));
var_dump(is_int(exit_status()));
?>
--EXPECT--
int(0)
bool(true)
bool(true)
21 changes: 21 additions & 0 deletions Zend/tests/exit_status/exit_status_error.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
--TEST--
exit_status() error with parameters
--FILE--
<?php
try {
exit_status(42);
echo "ERROR: Should have thrown ArgumentCountError\n";
} catch (ArgumentCountError $e) {
echo "PASS: Caught expected ArgumentCountError\n";
}

try {
exit_status(1, 2, 3);
echo "ERROR: Should have thrown ArgumentCountError\n";
} catch (ArgumentCountError $e) {
echo "PASS: Caught expected ArgumentCountError for multiple params\n";
}
?>
--EXPECT--
PASS: Caught expected ArgumentCountError
PASS: Caught expected ArgumentCountError for multiple params
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 {}

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.