php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #20556 php blocks strip newlines from output
Submitted: 2002-11-21 20:15 UTC Modified: 2002-11-22 12:07 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: php at 3wheel dot net Assigned:
Status: Not a bug Package: Output Control
PHP Version: 4.2.2 and 4.2.3 OS: FreeBSD 4.2-REL
Private report: No CVE-ID: None
 [2002-11-21 20:15 UTC] php at 3wheel dot net
This problem never shows up when using PHP to output HTML since it ignores newlines... but if you're outputting straight text it's a huge hassle.  Any block of php code placed at the end of a line (adjacent to a newline character) will "eat" that newline character.  Example:

********10<? ?>
********20

Give it a try, it outputs:

********10********20

No matter what is present in the php block this happens.  If there is a character directly following the block it will not get eaten:

********10<? ?>C
********20

Produces:

********10C
********20

This is a big annoyance for me, as I'm using PHP to generate text (as well as html) emails using templates.  I'm thinking the parser is just running off the end of the line looking for another php block, but it never bothers to put the newline back when it's done.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-11-22 05:00 UTC] derick@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php


 [2002-11-22 11:56 UTC] php at 3wheel dot net
I have read the php documentation more times than I care to count, and I have read the bug sumission guidlines.  I've also searched the documentation, the source, php.net, usenet, and Google to attempt to find any documentation or references as to why PHP would be removing newlines that it shouldn't be. Could you please clarify why this is not a bug? I urge you to reread my original submission, as well as my clarification below.  

I am NOT talking about HTML breaks, I am talking about newline (\n) characters.  This ticket has nothing to do with HTML output, it's just text processing.  Yes, I do understand how HTML breaks work.  Here's an HTML example anyways, if that helps:


-----cut here-----
<html>
<body>
<pre> 
this is on
two lines

this should also be<? ?>
on two lines

</pre>

this is on two lines,<br><? ?>
but somebody stole my \n


</body>
</html>
-----cut here-----
Run from the command line, this produces:

bash-2.04$ php test.php
X-Powered-By: PHP/4.2.2
Content-type: text/html

<html>
<body>
<pre> 
this is on
two lines

this should also beon two lines

</pre>

this is on two lines,<br>but somebody stole my \n

</body>
</html>






Or another example:

-----cut here-----
<? ob_start(); ?>

line1
line2<? ?>
line3<? ?>
line4

<? 
$lines = ob_get_contents();
ob_end_clean();


$lines = preg_replace("/\n/","NEWLINE\n",$lines);

print $lines;
?>
-----cut here-----

The output of this one SHOULD be:
NEWLINE
line1NEWLINE
line2NEWLINE
line3NEWLINE
line4NEWLINE
NEWLINE

(each line separated by a newline character)
BUT.. the actual output is:

NEWLINE
line1NEWLINE
line2line3line4NEWLINE
NEWLINE

The newlines that were present in the source file have mysteriously dissappeared after lines 2 and 3.  


I have also tested this under 4.2.3, and the bug is still present.
 [2002-11-22 11:58 UTC] derick@php.net
I guess you need glasses then, from:

http://www.php.net/manual/en/language.basic-syntax.php 

I quote: "The closing tag for the block will include the immediately trailing newline if one is present. Also, the closing tag automatically implies a semicolon; you do not need to have a semicolon terminating the last line of a PHP block."

 [2002-11-22 12:07 UTC] php at 3wheel dot net
Well, I'll call this a documented bug then.  If you guys want to see php be used for more than just outputting html, I suggest that you allow people to disable this behavior.  I see no good reason that php removes the newline, except the fact that this behavior was observed and nobody has figured out a way to fix it.  I'm using PHP templates to create text emails, and the fact that I have to add in extra newlines to account for the ones that php snarfs is pretty ludicrous.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Dec 26 23:01:28 2024 UTC