Daniel
Daniel

Reputation: 6842

PHP include causes white space at the top of the page

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.

Upvotes: 29

Views: 34942

Answers (15)

Mauro S
Mauro S

Reputation: 46

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.

Upvotes: 0

deibu
deibu

Reputation: 1

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

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

Upvotes: 0

gSeaner
gSeaner

Reputation: 1

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 (

Upvotes: 0

Frank Young
Frank Young

Reputation: 1

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.

Upvotes: 0

Leonardo Ciaccio
Leonardo Ciaccio

Reputation: 3136

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

Upvotes: 0

hagai shaul
hagai shaul

Reputation: 35

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".

Upvotes: 3

Victor Sitnic
Victor Sitnic

Reputation: 380

  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.

Upvotes: 10

Bruno
Bruno

Reputation: 1263

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.

Upvotes: 14

cebasso
cebasso

Reputation: 31

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...

regards

Upvotes: 3

Daniel
Daniel

Reputation: 6842

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

Upvotes: 41

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324840

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)

Upvotes: 4

irfanmcsd
irfanmcsd

Reputation: 6571

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/'
xmlns:fb='http://www.facebook.com/2008/fbml'>

Upvotes: 0

Dejo Dekic
Dejo Dekic

Reputation: 2126

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 :

*{margin:0;
 padding:0;
 }

Upvotes: 0

scunliffe
scunliffe

Reputation: 63676

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 &#65279; 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.

Upvotes: 3

aefxx
aefxx

Reputation: 25289

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.

Upvotes: 1

Related Questions