What is the purpose of using the sleep function before a header redirect in PHP?

When using a header redirect in PHP, it is important to use the sleep function before the redirect to ensure that any output buffering is flushed before the redirect occurs. This helps prevent unexpected behavior or errors that may occur if the redirect is sent before the output buffer is fully flushed.

ob_start();
// any output or processing here

// flush output buffer
ob_end_flush();

// delay redirect for 1 second to ensure buffer is flushed
sleep(1);

// perform header redirect
header("Location: new_page.php");
exit();