Skip to content

Commit 3b124ed

Browse files
Fix GH-15796: Add the exit_status() function
1 parent 8f1a627 commit 3b124ed

10 files changed

+101
-1
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ PHP NEWS
5858
. The socket_set_timeout() alias function has been deprecated. (timwolla)
5959
. Passing null to to readdir(), rewinddir(), and closedir() to use the last
6060
opened directory has been deprecated. (Girgias)
61+
. Added the exit_status() function to retrieve the current exit
62+
status. (alexandre-daubois)
6163

6264
31 Jul 2025, PHP 8.5.0alpha4
6365

UPGRADING

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,7 @@ PHP 8.5 UPGRADE NOTES
574574
- Standard:
575575
. Added array_first() and array_last().
576576
RFC: https://wiki.php.net/rfc/array_first_last
577+
. Added exit_status().
577578

578579
========================================
579580
7. New Classes and Interfaces
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--TEST--
2+
exit_status() basic functionality test
3+
--FILE--
4+
<?php
5+
var_dump(exit_status());
6+
var_dump(function_exists('exit_status'));
7+
var_dump(is_int(exit_status()));
8+
?>
9+
--EXPECT--
10+
int(0)
11+
bool(true)
12+
bool(true)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--TEST--
2+
exit_status() error with parameters
3+
--FILE--
4+
<?php
5+
try {
6+
exit_status(42);
7+
echo "ERROR: Should have thrown ArgumentCountError\n";
8+
} catch (ArgumentCountError $e) {
9+
echo "PASS: Caught expected ArgumentCountError\n";
10+
}
11+
12+
try {
13+
exit_status(1, 2, 3);
14+
echo "ERROR: Should have thrown ArgumentCountError\n";
15+
} catch (ArgumentCountError $e) {
16+
echo "PASS: Caught expected ArgumentCountError for multiple params\n";
17+
}
18+
?>
19+
--EXPECT--
20+
PASS: Caught expected ArgumentCountError
21+
PASS: Caught expected ArgumentCountError for multiple params
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
exit_status() with uncaught exceptions setting exit code
3+
--FILE--
4+
<?php
5+
register_shutdown_function(function() {
6+
echo "Exit status is: " . exit_status() . "\n";
7+
});
8+
9+
// set exit status to 255
10+
throw new Exception("Test exception");
11+
?>
12+
--EXPECTF--
13+
Fatal error: Uncaught Exception: Test exception in %s:%d
14+
Stack trace:
15+
#0 {main}
16+
thrown in %s on line %d
17+
Exit status is: 255
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
exit_status() in shutdown handler preserving original exit code
3+
--FILE--
4+
<?php
5+
register_shutdown_function(function() {
6+
echo "Current exit status: " . exit_status() . "\n";
7+
8+
$original_status = exit_status();
9+
$final_status = $original_status ?: 255;
10+
11+
echo "Final status will be: " . $final_status . "\n";
12+
});
13+
14+
echo "Setting exit status to 42\n";
15+
exit(42);
16+
?>
17+
--EXPECT--
18+
Setting exit status to 42
19+
Current exit status: 42
20+
Final status will be: 42
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--TEST--
2+
exit_status() with string exit codes
3+
--FILE--
4+
<?php
5+
register_shutdown_function(function() {
6+
echo "Exit status: " . exit_status() . "\n";
7+
});
8+
9+
echo "Before exit with string\n";
10+
exit("Error message");
11+
?>
12+
--EXPECT--
13+
Before exit with string
14+
Error messageExit status: 0

Zend/zend_builtin_functions.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,13 @@ ZEND_FUNCTION(error_reporting)
501501
}
502502
/* }}} */
503503

504+
ZEND_FUNCTION(exit_status)
505+
{
506+
ZEND_PARSE_PARAMETERS_NONE();
507+
508+
RETURN_LONG(EG(exit_status));
509+
}
510+
504511
static bool validate_constant_array_argument(HashTable *ht, int argument_number) /* {{{ */
505512
{
506513
bool ret = 1;

Zend/zend_builtin_functions.stub.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ function strncasecmp(string $string1, string $string2, int $length): int {}
4141

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

44+
function exit_status(): int {}
45+
4446
function define(string $constant_name, mixed $value, bool $case_insensitive = false): bool {}
4547

4648
function defined(string $constant_name): bool {}

Zend/zend_builtin_functions_arginfo.h

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)