What is the common error "cannot modify header information - headers already sent by" in PHP and how does it occur?

The common error "cannot modify header information - headers already sent by" in PHP occurs when there is output (such as echo, print, whitespace, or HTML) sent before PHP tries to modify headers using functions like header() or setcookie(). To solve this issue, make sure to avoid any output before calling functions that modify headers.

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

// Your PHP code here

ob_end_flush(); // Flush the output buffer