1

I've tried the following:

<?php
function shutdown_find_exit()
{
    exit(50);
}
register_shutdown_function('shutdown_find_exit');

trigger_error("Fatal error", E_USER_ERROR);

?>

But that still exists with an error code of 255.

I'm trying to have an error code of 50 returned.

I'm checking it with the following bash script:

php tester.php
status=$?
echo Exit Code: ${status}

Updated code based on the accepted answer for anyone interested:

<?php
function shutdown_find_exit()
{
    exit('50');
}
register_shutdown_function('shutdown_find_exit');

trigger_error("Fatal error", E_USER_ERROR);
?>

and the bash script

output="$(php tester.php)"
status=$?
echo Exit Code: ${status}
if [[ $output == *"50"* ]]
then
  echo "Got 50";
fi
1
  • Calling the shutdown functions is just the first of a dozen things PHP does on script termination/exit. Which is why the shutdown callbacks can't override the exit code anymore I'd guess. (Btw, 70 would be a better result code, EX_SOFTWARE.) Commented May 14, 2015 at 15:23

2 Answers 2

1

TL;DR: use a string, instead of an int.

php exit( status )

If status is a string, this function prints the status just before exiting.

If status is an integer, that value will be used as the exit status and not printed. Exit statuses should be in the range 0 to 254, the exit status 255 is reserved by PHP and shall not be used. The status 0 is used to terminate the program successfully.

Note: PHP >= 4.2.0 does NOT print the status if it is an integer.

Execute php script in bash, assign output to variable.

variable = $(/path/to/php -f $HOME/path/to/my.php)

For example:

# just executing php, but capturing output then, prints it to console
status = $(/usr/bin/php -f $HOME/path/to/tester.php)
echo Exit Code: ${status}

Hope this helps.

Sign up to request clarification or add additional context in comments.

4 Comments

If I use a string, that string is displayed, but the exit code captured by the bash script is still 255.
@Rob: i've updated with an example maybe useful to you.
I appreciate the help, I updated the original question showing the code I used.
Nice! Thanks for sharing.. pay it forward :)
1

See if this does what you want to do:

trigger_error("Fatal error", shutdown_find_exit());

1 Comment

This actually did work, but I was just using the trigger_error as an easy way to trigger the error to make the example shorter. I apperciate the effort, but I'm going to mark Adrian's because that answer worked better for me.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.