Common warning when working with PHP can be:
Warning: Cannot modify header information - headers already sent by (output started at /var/www/site/public_html/index.php:60)
Functions that send or modify HTTP headers must be called before any output is made:
HTTP specification defines that HTTP headers must always be sent before the output. PHP scripts mainly generate HTML content, but also pass a set of HTTP/CGI headers to the webserver.
Usual HTTP response looks like this:
HTTP/1.1 200 OK
Vary: Accept-Encoding
Content-Type: text/html; charset=utf-8
<html>
<head>
<title>Example</title>
</head>
<body>
<h1>Page title</h1>
<p>After the two linebreaks HTTP response output is defined.</p>
In above case first three lines are HTTP headers and after two line breaks output is defined. When PHP script receives the first
output (html, output from echo
function etc) it will also flush all collected headers so far in the script procedure. Afterwards
it can send all the output it wants however adding other HTTP headers will be impossible from there on.
<?php
tag or after closing ?>
tag. <?php
session_start();
UTF-8 Byte Order Mark
. PHP files should be encoded with UTF without BOM
.echo
, print
, printf
or other printing functions produce output.<html>
sections prior to <?php
code are written.