Consider the following code:
<div id="sidebar">
<?php
require_once('/components/search.php');
require_once('/components/categories.php');
?>
</div>
search.php
and category.php
are essentially the same structure - a div container with some specific contents. Nothing special here, pure HTML:
<div class="component">
<!-- blah -->
</div>
However, when inserted with require_once
(or require
/ include
etc), PHP adds whitespace above each element, pushing it down, identifiable as an empty text node in Chrome's Inspect Element tool (the whitespace disappears when this node is deleted)
Deleting all unnecessary whitespace from the sidebar script (making it a single line of code) doesn't fix it. And if I just replace the require_once
lines with the contents of the components, the whitespace doesn't appear. So not sure why PHP is adding it on require. Any ideas?
Update
This one's still proving to be a weird one. I agree now that require_once
does not seem to be the root cause as such. I decided to ignore the problem for a while and hope it would go away after I'd worked on it further. Alas, it remains, so I did bit more investigating. Checking the page source in the browser confirms that the code in question is indeed returned as a single long unbroken line http://pastebin.com/dtp7QNbs - there's no whitespace or carriage return between any of the tags, yet space appears in the browser - identifiable in the Inspect Element tool as empty lines between each <div class="component">
Does this help shed any more light on the issue?
I had the same problem and verified Kai's solution to change the format to ANSI but also found that "Encode in UTF-8 without BOM" also works. This comes up as the default format for new Notepad++ PHP files so one less conversion step.
It seems that use of the byte order mark file header in UTF-8 is not generally recommended. I verified that my installation of VS2010 was adding BOM when saving PHP files.
The following stackoverflow article explains nicely where the extra whitespace got inserted.