PHP shutdown handlers and exit codes

I was in bed trying to get to sleep but my brain wanted to know the answer to this question. So I was forced out of bed to write this experiment:

function main( $argv ) {

  register_shutdown_function( 'shutdown_1' );
  register_shutdown_function( 'shutdown_2' );

  exit( 0 );

}

function shutdown_1() {

  exit( 1 );

}

function shutdown_2() {

  exit( 2 );

}

main( $argv );

With this PHP code, what do you expect is the resultant error level?

The answer is ‘1’. After main() calls exit( 0 ) the shutdown function shutdown_1() is invoked. When shutdown_1() calls exit( 1 ) the process exists and shutdown_2() is never called.

I’m glad we cleared that up. Back to bed.

Leave a Reply