The Problem With HTTP Headers When Using Include in PHP
The problem with HTTP headers can also
occur when including files via
include
. It manifests itself in the case
when after the closing tag ?>
there are spaces or blank lines,
like this:
<?php
// some code
?>
In this case, when including our file into another file, the left blank lines will act as output to the screen:
<?php
include 'file.php'; // inside there is output to the screen
header('Content-Type: text/html');
?>
To fix the problem, it is better in all
PHP files to remove the last closing
?>
. This technique does not lead to a PHP error
and at the same time protects us from accidentally
adding blank lines. Let's fix
our file:
<?php
// some code
Fix the errors in the following code:
<?php
function func1() {
echo '1';
}
?>
<?php
function func2() {
echo '2';
}
?>
<?php
include 'file1.php';
include 'file2.php';
header('Content-Type: text/html');
?>
Fix the errors in the following code:
<?php
function func1() {
echo '1';
}
?>
<?php
function func2() {
echo '2';
}
?>
<?php
include 'file.php';
header('Content-Type: text/html');
?>