开发者

php include causes unwanted newline

开发者 https://www.devze.com 2023-03-27 03:51 出处:网络
(PHP 5.3.6) I have a php file which contains simply this text - there are no php tags, no trailing newline or extraneous whitespace anywhere:

(PHP 5.3.6)

I have a php file which contains simply this text - there are no php tags, no trailing newline or extraneous whitespace anywhere:

<div style="border:1px solid green">abc</div>

Now including this from another php file as follows (again, with no extraneous whitespace anywhere):

<div style="border:1px solid red"><?php include "abc.php" ?></div>
<br />
<div style="border:1px solid red"><div style="border:1px solid green">abc</div></div>

I get the result below.

php include causes unwanted newline

Note that the second method just uses the included content directly. These should both be like the lower one, but as you can see the include causes some wierd kind of newline to be inserted befor开发者_开发知识库e the content of the included file. I say 'wierd' because when I check the outputted source (via Chrome's view source) there is nothing visible there:

php include causes unwanted newline

When this section of the page is shown in Chrome's element inspector, there seems to be something there but what exactly it is I can't tell:

php include causes unwanted newline

It appears to be simply an empty string, but why an empty string would cause newlines and why it would be there in the first place are a mystery. Adding a semicolon to the end of the include statement makes no difference. It occurred to me that it might be a null byte or a 13 (CR) but that should still not cause an HTML line break.

Does anyone know how I can get rid of this unwanted newline?


Check the encoding of the included abc.php - does it have a Byte-Order Mark (BOM)? If so, remove it (good code editors allow you to change the file encoding in the Save dialog), that could be the culprit.


Relying on the Chrome inspector to check the raw output is not a good idea, as the tree is formatted. Use show source is slightly better.

It's most probably not related to include() itself. The first step to take is to open your included file with a hex editor and check whether it is really empty. As Jens Roland pointed it, it can contain a BOM for example, which will be hidden by most text editors.

You can also generate a raw abc.php file with this code and test your code against it:

file_put_contents('abc.php', 'abc');


I was in same problem. I found the problem in editor. please edit and save your included file by your notepad or text editor. You will see the change.

0

精彩评论

暂无评论...
验证码 换一张
取 消