Home » Php » PHP include causes white space at the top of the page

PHP include causes white space at the top of the page

Posted by: admin April 23, 2020 Leave a comment


I have a problem on a site that I am making. There is this line of white space at the top of my page. I can not find out where it is coming from. It might be because of extra line breaks in my page thanks to php include() or maybe it is just some faulty css.

How to&Answers:

I got it! I must admit, this was a very strange one.

This was my exact problem, and his solution fixed it perfectly.

The problem was because of the include().

When you save a page as UTF-8, a special signature called a byte-order mark (or BOM) is included at the beginning of the file, indicating that it is a UTF-8 file. You can only see BOMs with low level text editors (such as DOS edit). You need to remove this signature from the included file in order to get rid of the white space at the top of the page.

DOS edit


In Notepad++ you can change the encoding of the file to “UTF-8 without BOM” from the “Encoding” menu. This fixes the problem for me.


  1. Open file in Notepadd++

  2. From top bar: Encoding->Encode in UTF8 without BOM

  3. Save the file

In Dreamweaver

Press CTRL + J or go to Change->Page Settings->Tile/Encoding and unchek UNICODE BOM if it is cheked.


Well you’re missing a DOCTYPE for starters… You also don’t have a character encoding specified.

Aside from that, make sure that if you’ve saved the files as UTF-8, they must not have a BOM at the start. Also ensure that you didn’t leave any empty lines (although whitespace is probably ignored, otherwise every layout I’ve ever written would break)


For some reason after your body tag before the first div you have some funky character… just delete them and re-indent as needed. the character  to be precise… (you can see it in Firebug if you inspect the HTML for the page…but you can’t see it in the view-source because it renders as whitespace).

unrelated to the alignment issue… you also have a miss-matched closing </li> tag after your login/signup link.


you can solve this problem doing the following…

based in what scunliffe said… you can avoid this character… example

$file = file_get_contents('header.php');
$file = substr($file, 3, strlen($file));
echo $file;

so, you’re skipping this bug…



Important: If you have multiple php pages as I have in my website using utf-8, you need to make sure you change the encoding of every single page to be “Encode in utf-8 without a BOM”.


Yep, there’s extra whitespace included before anything else.

May I recommend the use of output buffering to you? You will gain a fine grained control of what is pushed to the client.


After you declare a DOCTYPE make sure to add CSS reset into the first line of your CSS code to make it consistent across all browsers :



While reviewing your website output on firebug, i found small issue on top of html rendering. view in screen shot.
firebug report

It may be due to missing doctype on top of page. Add the following doctype on top of page and see output again.

<!doctype html>
<html itemtype="http://schema.org/WebPage" xmlns="http://www.w3.org/1999/xhtml"   xmlns:og='http://opengraphprotocol.org/schema/'


Tested all suggestion online, solved with notepad, procedure :

  1. Copy all content of the file to include, from php without <?
  2. Open Notepad and type <? then paste all code
  3. Enjoy


This problem has also bothered me for a long time, until I found a very simple solution.

This problem is caused by a BOM character, which is used to tell code page and which endian is of a text file. When you include a PHP file, this character will be included as well. Although you will not see the BOM character, but it might cause gap on your page.

My solution is here.
You don’t have to remove this character from files. What you have to do is like this:

<!-- <?php
include_once (dirname(__FILE__) . "/_include.php");
?> -->

Using HTML comment mark to wrap php code which includes other php files. And the BOM character will be considered as comment and will not appear on page.


BOM Character creates that problem. Sometimes it does not create such visual character that one see. Just check the file editor settings and set to “no BOM” to prevent such blank line.

Sometimes images won’t work because of this character, since the character set of image is corrupted by such character.

To avoid such situations,
i). check if your Code Editor creates new file with BOM. If so, change this setting.
ii). Open all the files of your code and check BOM character at start (


Try this this is the best idea that I know, cheers

<div style="position:absolute;">
<?php include("conn.php"); ?>
<!DOCTYPE html>


I just downloaded Notepad++ and should advise UTF-8 without BOM is not an encoding option. Choose only “UTF-8” not the “UTF-8 BOM”. That worked.