How can error_reporting() function help in debugging PHP code and identifying issues like headers already sent?

The error_reporting() function in PHP can help in debugging code by displaying or logging errors that occur during script execution. To identify issues like "headers already sent", you can set the error_reporting level to include warnings and notices, which will alert you to potential problems in your code. This can help pinpoint where the issue is occurring and aid in resolving it.

<?php
// Set error reporting to display warnings and notices
error_reporting(E_ALL);

// Code that may cause "headers already sent" issue
echo "Hello, World!";
header("Location: index.php");
?>