Php include causes of unwanted linefeeds

(PHP 5.3.6)

I have a php file that simply contains this text: there are no php tags anywhere, without a trailing new line or extraneous spaces:

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

Now including this from another php file as follows (again, without extraneous spaces 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.

Note that the second method directly uses the included content. They should be similar to the bottom, but, as you see, include leads to the fact that some wierd newline type is inserted before the contents of the included file. I say "wierd" because when I check the source source (via the Chrome view source), nothing is visible there:

enter image description here

When this section of the page appears in the Chrome inspector, there seems to be something, but what exactly cannot I say:

enter image description here

It seems to be just an empty line, but why an empty line will cause new lines and why it will be there is, first of all, a mystery. Adding a semicolon to the end of the include statement does not matter. It occurred to me that this could be null byte or 13 (CR), but that should not cause HTML line breaks anyway.

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

+4
source share
3 answers

Check the encoding of the included abc.php - does it have a byte sign (BOM)? If so, delete it (good code editors allow you to change the encoding of a file in the Save dialog box), which may be the culprit.

+14
source

Relying on the Chrome inspector to check the original output is not a good idea since the tree is formatted. Using a source of evidence is slightly better.

This is most likely not related to include() . The first step is to open the file with the hex editor turned on and check if it is really empty. As Jens Roland pointed out, it may contain a specification that will be hidden by most text editors.

You can also create the source abc.php file with this code and test your code against it:

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

I had the same problem. I found the problem in the editor. edit and save the attached file in your notepad or text editor. You will see the changes.

0
source

All Articles