PHP header redirect problem

Few days back when I was writing some PHP, I found out that

header( 'location: xyz.php' );

was not working on my hosting. The problem was headers were being sent by the server before processing the entire php. So I added this function at the top of the php file to buffer the output stream,

ob_start();

and the below function at the bottom of the php file to flush the buffer.

ob_end_flush();

This solved my problem.