Skip to content

Commit 22a19ee

Browse files
Fix GH-15796: Add the exit_status() function
1 parent 2b5d978 commit 22a19ee

8 files changed

+68
-1
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ PHP NEWS
7373
opened directory has been deprecated. (Girgias)
7474
. Fixed bug GH-19153 (#[\Attribute] validation should error on
7575
trait/interface/enum/abstract class). (DanielEScherzer)
76+
. Added the exit_status() function to retrieve the current exit
77+
status. (alexandre-daubois)
7678

7779
31 Jul 2025, PHP 8.5.0alpha4
7880

UPGRADING

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,7 @@ PHP 8.5 UPGRADE NOTES
593593
- Standard:
594594
. Added array_first() and array_last().
595595
RFC: https://wiki.php.net/rfc/array_first_last
596+
. Added exit_status().
596597

597598
========================================
598599
7. New Classes and Interfaces
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)