php include causes unwanted newline - php

(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.
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 before 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:
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:
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.

Related

HTML on the website missing a line break from HTML in .php file

In my Notepad++ UTF8 encoded (no BOM) PHP file, there is CRLF after each line. On the front end, one of them simply disappears resulting in HTML with the line break missing. This is not echoed, it's HTML in a PHP file. Any idea why it would happen and how to solve it?
I selected the relevant piece of this file. The important thing is that the button and the div tags are all display: inline-block styled and I expect to have a line break between them, in order for the gaps to be displayed. The gap shows up only between the div and the 2nd button because the line break is missing on the front end for line 39.
If I do any change to this PHP file, it is correctly shown on the website, and this line break is "since always", so it's not a caching problem.

body in a php file

I created a custom index.php for a wordpress theme. I just renamed the .html to .php file. Everything seems to work fine except there are extra characters printed if I run the page.
These characters are printed at start of the body area in the browser : " --> "
I am confused as to from where these characters are printed. I can create a .php with complete html contents right? Or do I need to do some modification.
<!--this is a HTML comment line -->
If you forget to delete last --> characters after deleting the first part, you might be seeing that. We cannot know without seeing your code.
As answer to the last question, you can mix php and plain HTML. Whenever you are writing php your code must be within
<?php ... CODE HERE ... ?>
Inline php however is not a good programming pattern in my opinion.

PHP adding empty text node before including html file

I'm using php as templating engine, and I've noticed that when I include view file, empty text node is added before content of that view.
For example, I have html file I want to include that has following content:
<p>Some text</p>
than I include that file like this:
<div><?php require_once('file/path.htm'); ?></div>
(notice that I've removed any spaces between div and php) And after php includes file he adds empty text node (which I'll mark like this "") that adds space before p tag, so I get something like this:
Some previous content...
<div>
"" //empty text node
<p>Some text</p>
</div>
This is quite problematic since it ruins content composition. Is there any solution to this?
FSou1 has it right, it's the charset, it can also be solved by saving as UTF-8 without BOM:
Open your PHP inlcude file in Notepad++ (download here: http://notepad-plus-plus.org/)
Select Encoding --> Encode in UTF-8 without BOM
Empty nodes disappear. Hope that helps someone. This was driving me crazy.
I had the same problem right now, and i had a luck when find answer. There answer is in charset. It could be strange, but when you save your file in UTF-8, you have empty in your markup. When your file in cp1251, you dont have this problem.
This was my second issue caused by BOM (both took over an hour of debugging, Googling and hairpulling).
I just found this (windows-only) small drag and drop program that check for BOM which it can remove:
File BOM Detector by Brynt Younce
Softpedia.com/get/System/File-Management/File-BOM-Detector.shtml
Small, easy and simple. There seems to by a PHP solution for all platforms bu I have not tested it.
Take a look if interested:
Github.com/emrahgunduz/BomCleaner

PHP code line break `\n` causing gap between elements

I'm echoing a series of HTML elements using PHP. I'm using \n to cause code line breaks to make the source code more organized and legible.
For some reason, the use of \n in a specific location is causing a mysterious gap between the HTML elements. In firebug, this gap is not showing up as a margin, or padding, but rather just a gap.
Here is the PHP in question:
Note: As you can see, I have removed all of the PHP inside the tags as I'm pretty sure it is not relevant to this problem.
echo '<ul ... >'."\n";
while($row = mysql_fetch_assoc($result_pag_data)) {
echo '<li><a ... >'."\n".
'<img ... >'."\n".
'</a></li>'."\n"; <---- THIS IS THE \n THAT SEEMS TO BE CAUSING THE GAP
}
echo '</ul>'."\n";
Have you ever seen anything like this before, a presentation gap associated with PHP line breaks?
If so, what is the reason for it?
Is it really that important that I use \n in my code?
That's normal. A \n line break has no meaning in HTML, so it's interpreted as a space character. If you don't want that gap, then eliminate the \n, or rewrite the html so it's not relevant:
<li><a ...><img ...></a></li>
As a general rule, tags which can contain text should never have their closing tags on a line by themselves, for this very reason.
Following up on your 'where to put \n' question. This comes down to personal preference, but I tend to format my html like this:
<table>
<tr>
<td><a href="some big long ugly url">
<img ....></a></td>
</tr>
Since <tr> can't contain any text on its own (in valid html), it's ok to put on its own line. But the </a> and </td> are both tags that CAN contain text, so I put them right up against the end of the 'text' (the img tag in this case), so that the Phantom Linebreak Menance (coming soon to a starwars ripoff near you) can't strike.
Note, of course, that my example does have a line break and indentation between the opening <a> and the <img> tag, so that's another place where a "must be right next to each" other layout would cause a gap. If you need a series of things lined up smack dab against each other, than you basically can't use line breaks anywhere in that section of the page.
The whitespace is translated into (empty) HTML text nodes, which take up some space (you can test this by walking the DOM). There is no solution to make these disappear that I know of other than removing the whitespace from your HTML in the first place.
Of course it's not only \n that would cause this behavior; spaces or tabs would do exactly the same as well.
In that particular case the newlines are used to prettify the html source, keep it readable via view-source. That's quite common actually. (Yet redundant.)
As said by the other answers, it does not have meaning normally. Albeit this can be overriden via CSS and the attribute (which we can assume is not the case here):
white-space: pre-line;
You should only output a newline where you in fact want a newline in the output. In HTML, a newline is whitespace, just like the space character.

New line formatting when using HTML file as Word file?

I'm writing a PHP application for a client that needs a pre-existing HTML page I've already created to be "exported" as an Word file. Simply, this is how it's done:
if (isset($_GET["word"])) {
header("Content-type: application/vnd.ms-word");
header("Content-Disposition: attachment;Filename=some_file.doc");
}
This, of course, will be called if a "word" flag is located in the page querystring, e.g.:
whateverpage.php?somequery=string&someother=test&word
Anyways, my question is, despite how complex this HTML page actually is, it actually transfers pretty well to a nicely formatted Word file just by changing the content-type. The only problem I'm having is that new line breaks (HTML <br> tags) aren't formatting properly. E.g.: In my html, if I have something that looks like
Aug
01
with a BR between the lines, it always ends up showing
Aug 01
in the generated Word file.
I've done some Googling and lots of tests with various other things but nothing seems to format properly with a simple new line.
Does anyone know how to properly format a new line character in a Word file that's being created from an HTML file?
Any help is greatly appreciated.
Edit:
I've tried wrapping the said line in a P tag, ala:
<p>Aug<br>01</p>
Without luck. I've also tried making a basic document and Word, saving it as an HTML file and looking at the generated (i.e sloppy) Word HTML source. There is some CSS in there that I thought might give me a clue, but I tried everything and nothing seemed to work properly. Word seems to add an 'MsoNormal' class to wrapped paragraphs, I tried adding this but it just removes any font formatting I had and doesn't help. Here is the CSS Word creates itself:
p.MsoNormal, li.MsoNormal, div.MsoNormal
{mso-style-unhide:no;
mso-style-qformat:yes;
mso-style-parent:"";
margin-top:0cm;
margin-right:0cm;
margin-bottom:10.0pt;
margin-left:0cm;
line-height:115%;
mso-pagination:widow-orphan;
font-size:11.0pt;
font-family:"Calibri","sans-serif";
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-fareast-font-family:Calibri;
mso-fareast-theme-font:minor-latin;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;
mso-bidi-font-family:"Times New Roman";
mso-bidi-theme-font:minor-bidi;
mso-fareast-language:EN-US;}
I had this same problem, I was tagging my line breaks like so:
<br/>
When I changed it to just
<br>
Then my line breaks starting working.
Your problem is probably due to the fact that when you switch the content type to a Word document, the browser doesn't render it as HTML. My guess is that you need to add a newline to the Word document if you want a line break.
How to insert this line break? I'm not sure, but you could always try:
echo "Aug\r\n01";
Where \r\n are the newline characters.
How about, if you want to maintain a line-break, just echo "<p>Aug</p><p>01</p>"; it ain't pretty, but it should effect the line break you're looking for.

Categories