How can output buffering in PHP help prevent header already sent errors when using header() for redirection?

When using header() for redirection in PHP, it is important to ensure that no output is sent to the browser before the header is set. Output buffering in PHP can help prevent header already sent errors by capturing all output before sending it to the browser. This allows you to set headers at any point in your code without worrying about potential errors.

<?php
ob_start(); // Start output buffering

// Your PHP code here

ob_end_flush(); // Send output buffer to the browser