PHP include 导致页面顶部出现空白
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10199355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP include causes white space at the top of the page
提问by Daniel says Reinstate Monica
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.
我在制作的网站上遇到问题。我的页面顶部有一行空白。我不知道它来自哪里。这可能是因为我的页面由于 php 出现了额外的换行符,include()或者可能只是一些有问题的 css。
回答by Daniel says Reinstate Monica
I got it! I must admit, this was a very strange one.
我知道了!我必须承认,这是一个非常奇怪的问题。
Thiswas my exact problem, and his solution fixed it perfectly.
The problem was because of the include().
问题是由于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.
将页面另存为 UTF-8 时,文件开头会包含一个称为字节顺序标记(或 BOM)的特殊签名,表明它是 UTF-8 文件。您只能使用低级文本编辑器(例如DOS edit)查看 BOM 。您需要从包含的文件中删除此签名,以去除页面顶部的空白区域。


回答by Bruno
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.
在 Notepad++ 中,您可以从“编码”菜单中将文件的编码更改为“无 BOM 的 UTF-8”。这为我解决了问题。
回答by Victor Sitnic
Open file in Notepadd++
From top bar: Encoding->Encode in UTF8 without BOM
Save the file
在记事本++中打开文件
从顶部栏:编码-> 无 BOM 的 UTF8 编码
保存文件
In Dreamweaver
在 Dreamweaver 中
Press CTRL + J or go to Change->Page Settings->Tile/Encodingand unchek UNICODE BOMif it is cheked.
按 CTRL + J 或转到Change- >Page Settings->Tile/Encoding并取消选中UNICODE BOM(如果已选中)。
回答by Niet the Dark Absol
Well you're missing a DOCTYPE for starters... You also don't have a character encoding specified.
好吧,您缺少初学者的 DOCTYPE ……您也没有指定字符编码。
Aside from that, make sure that if you've saved the files as UTF-8, they must not have a BOMat 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)
除此之外,请确保如果您已将文件保存为 UTF-8,则它们在开始时不得有BOM。还要确保您没有留下任何空行(尽管空格可能会被忽略,否则我写过的每个布局都会中断)
回答by cebasso
you can solve this problem doing the following...
您可以执行以下操作来解决此问题...
based in what scunliffe said... you can avoid this character... example
根据 scunliffe 所说的......你可以避免这个角色......例子
$file = file_get_contents('header.php');
$file = substr($file, 3, strlen($file));
echo $file;
so, you're skipping this bug...
所以,你正在跳过这个错误......
regards
问候
回答by scunliffe
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 HTMLfor the page...but you can't see it in the view-source because it renders as whitespace).
出于某种原因,在第一个 div 之前的 body 标签之后,你有一些时髦的角色......只需删除它们并根据需要重新缩进。准确的字符...(如果您检查页面的HTML,您可以在 Firebug 中看到它...但您无法在视图源中看到它,因为它呈现为空白)。
unrelated to the alignment issue... you also have a miss-matched closing </li>tag after your login/signup link.
与对齐问题无关……您</li>在登录/注册链接后还有一个不匹配的结束标签。
回答by hagai shaul
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 pageto be "Encode in utf-8 without a BOM".
重要提示:如果您在我的网站中使用 utf-8 有多个 php 页面,则需要确保将每个页面的编码更改为“在没有 BOM 的情况下以 utf-8 编码”。
回答by aefxx
Yep, there's extra whitespace included before anything else.
是的,在其他任何内容之前都包含额外的空格。
May I recommend the use of output bufferingto you? You will gain a fine grained control of what is pushed to the client.
我可以向您推荐使用输出缓冲吗?您将获得对推送给客户端的内容的细粒度控制。
回答by Dejo Dekic
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 :
声明 DOCTYPE 后,请确保将 CSS reset 添加到 CSS 代码的第一行,以使其在所有浏览器中保持一致:
*{margin:0;
padding:0;
}
回答by irfanmcsd
While reviewing your website output on firebug, i found small issue on top of html rendering. view in screen shot.

在 firebug 上查看您的网站输出时,我发现了 html 渲染上的一个小问题。在屏幕截图中查看。

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

