Example 1 — Check Before Sending a Header
Test headers_sent() and only call header() when it is still safe.
<?php
if (!headers_sent()) {
header("X-Safe: 1");
echo "header queued", PHP_EOL;
} else {
echo "too late for headers", PHP_EOL;
} How It Works
- At the start of the script, headers usually are not sent yet.
- The if condition passes, so header() can run.
- We print a short confirmation message.
- This guard prevents "headers already sent" errors in larger apps.

