What is the purpose of sending a header in PHP?
Sending a header in PHP is used to send additional information to the browser before any other output is sent. This is commonly used to set HTTP headers such as content type, redirecting to a different page, or setting cookies. It is important to send headers before any output is sent to avoid any errors.
<?php
// Set content type to JSON
header('Content-Type: application/json');
// Redirect to a different page
header('Location: https://www.example.com');
// Set a cookie
setcookie('user', 'John Doe', time() + 3600, '/');
?>