What are some potential pitfalls to be aware of when using the header() function in PHP?
One potential pitfall when using the header() function in PHP is that it must be called before any output is sent to the browser. If any output is sent before the header() function is called, it will result in a "headers already sent" error. To avoid this issue, make sure to call the header() function before any HTML, whitespace, or other content is echoed or printed.
<?php
// Incorrect usage that will result in "headers already sent" error
echo "Hello, World!";
header("Location: index.php");
// Correct usage
header("Location: index.php");
exit;
?>