Можно так:
function sendResponseAndContinue(string $responce = 'Done') {
ob_start();
print($responce);
header('Connection: close');
header('Content-Length: ' . ob_get_length());
ob_end_flush();
@ob_flush();
flush();
fastcgi_finish_request();
}
// Пример использования:
sendResponseAndContinue(); // отправиляем ответ
// делаем, что нужно
exit; // завершаем скрипт
также можно прочитать про фукции и использовать их: