I was trying to figure out how to set HTTP status header to 500 if there was php fatal error (for handling AJAX requests).
Related question: How can I get php to return 500 upon encountering a fatal exception?
I don’t want to use register_shutdown_function which in my opinion is not an elegant solution.
Is below code a good solution?
Will it always work for fatal errors?
I assume that application code has no output untill last echo call and there are no calls to flush() function (which after invocation prevents modifying headers).
<?php
set_status_code(500);
// ...
// (application code with no output)
// Application didn't crash up to this point,
// so we set http status code 200.
set_status_code(200);
echo $outputBuffer;
Is flush() the only function that breaks desired behaviour?
If you turn off PHP errors, Apache will give a 500 status code whenever a fatal PHP error occurs.