1

It looks like I'm missing some basics here. Sorry, If question may seem trivial, but I'm loosing my wits looking for any cause of that, with no luck for hours now.

It seems like including more than one file in already included file is causing some minor change to the page layout. For this very simple example it just shifts first line a bit (like a space-character sign is added), but for my more complex site this "flaw" behaves like a line break and it puts whole website for a line down (breaking my website's style).

Here's example. I have uber-simple main PHP file("include_test.php"):

<?php require ("./file.php");?>
<!doctype html>
<html>
    <head></head>
    <body>
        This is a first line of html body.<br>
        This is a second line of html body.<br>
        This is a third...<br>
    </body>
</html>

Then required file - "file.php", looks like this:

<?php
    require_once("file2.php"); 
    require_once("file3.php"); 
?>

And then both required file above -file2.php and file3.php. It seems irrelevant here, as I can put any content there with no change, but either way they are such super simple php files -

file2.php:

<?php
    function alfa(){
        return 1;
    }
?>

file3.php:

<?php
    function omega(){
        return 1;
    }
?>

Now please look at the output of main "include_test.php: here:

http://destadesign.com/tag/include_test.php

Notice the slight shift of the start of the body content? What is causing this?

Note: I've worked out that removing any of the "require" statements fix this out. Remove second "require" statement of file.php "require_once("file3.php");" - fixed. Remove first "require" statement of file.php "require_once("file2.php");" - fixed. Remove "require" statement of include_test.php "require("file.php");" - fixed.

Conclusion (however weird) seems that two require statements (or include - as I've checked that too - or include_once, or require_once too) in the same file which is also included outputs some invisible, unexpected and unjustified character. Now is it correct or am I missing something obvious here ?

Jaak Kütt
  • 2,239
  • 4
  • 26
  • 37
Jan Desta
  • 489
  • 5
  • 13
  • 1
    What counts is the resulting HTML output. What does it look like with and without those includes? – Pekka Dec 28 '13 at 18:49
  • Also no PHP erros (including notices) are generated with error reporting set to E_ALL. – Jan Desta Dec 28 '13 at 18:49
  • With includes: http://destadesign.com/tag/include_test.php - an empty character added. Without one include in file.php: http://destadesign.com/tag/include_test2.php - proper HTML output. – Jan Desta Dec 28 '13 at 18:50
  • Then remove that empty character. It's likely to be the line break after the ` ?>` in the first line. – Pekka Dec 28 '13 at 18:53
  • I've removed line breaks in all three included files. They are so simple, it's hard to miss anything. I've put all scripts in one line, removed spaces and line-breaks, but this gap is there. It looks like it's connected with include/require statements. Are you aware of any restrictions/implications of icnluding files within included files/ including multiple files at once ? some collisions when icluding many files? it doesn't look like html issue, but php. – Jan Desta Dec 29 '13 at 13:14
  • 1
    You may have a line break at the end of one of the files you're including. Remove the `?>` altogether to avoid that. (PHP will interpret the file's end as a `?>` automatically). – Pekka Dec 29 '13 at 16:45
  • 1
    Pekka, thank you for your help and engagement. It's been very instructing. I've removed all php code ending tags ("?>"), but it didn't solve the problem. I've also removed ALL break lines from code using TextWrangler tool (just to be sure there is absolutely no break lines). Also this did not helped. However I did find the cause of this and solution to my problem - I'll write it as asnswer to this question, so it'll be more clear to read this for others. – Jan Desta Dec 30 '13 at 14:12
  • Great! I'll edit the title to make the question more easy to find for future readers. Feel free to roll back the edit if you don't like it. – Pekka Dec 30 '13 at 15:17

1 Answers1

1

Cause of such problem is not in the code, this does not involves line-breaks in html or php code, nor whitespaces. Such things happens because of the type of the included files. Particularily their encoding as UTF-8 with BOM.

When one of the included file has BOM signature, it may produce such unexpected and hard to explain whitespace or break-characters, as to why I do not know exactly (seems like it's treated randomly by browsers). So I've open and saved all files (file.php, file2.php, file3.php) as UTF-8 (without BOM) and this solved my problem here.

About BOM: http://en.wikipedia.org/wiki/Byte_order_mark

Regarding to this topic - What's different between UTF-8 and UTF-8 without BOM? - I am treating this BOM signature as unnecessary (as I do not use Hebrew characters encoding or such exotic things), and in my situation harmful.

A good day to all readers! :) J

Community
  • 1
  • 1
Jan Desta
  • 489
  • 5
  • 13