php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #41281 evaluating or including php content loses newlines
Submitted: 2007-05-04 10:29 UTC Modified: 2007-05-04 12:41 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: fantasticjamieburns at hotmail dot com Assigned:
Status: Closed Package: Documentation problem
PHP Version: 4.4.7 OS: Linux/Windows
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: fantasticjamieburns at hotmail dot com
New email:
PHP Version: OS:

 

 [2007-05-04 10:29 UTC] fantasticjamieburns at hotmail dot com
Description:
------------
Well when I eval() or include() the code below, a newline gets truncated. Basically, whenerver an "end of php block" is the last thing on a line, the parser steals a newline.

Reproduce code:
---------------
ONE
TWO<?php /* whatever */ ?>
THREE

Expected result:
----------------
ONE
TWO
THREE

Actual result:
--------------
ONE
TWOTHREE

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-05-04 10:42 UTC] tony2001@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


 [2007-05-04 10:51 UTC] fantasticjamieburns at hotmail dot com
I have been developing with PHP since version 3.

I have read the manual many times.

The manual states:

"... when PHP hits the ?> closing tags, it simply starts outputting whatever it finds until it hits another opening tag."

NOW...

As I have highlighted, PHP is not outputting everything after the closing tag in the specific case where there is a newline immediately after the closing tag.

So... either tha manual is wrong or the parser is broken.

Did you even bother to try the simple test case I gave?
 [2007-05-04 11:02 UTC] derick@php.net
But the new line is part of the closing tag. This is however not properly documented here: http://www.php.net/manual/en/language.basic-syntax.php
 [2007-05-04 11:03 UTC] tony2001@php.net
Reclassified as docu problem.

 [2007-05-04 11:21 UTC] fantasticjamieburns at hotmail dot com
OK, thanks for classifying this as a documentation problem.

If it is indeed the case that a PHP end tag considers a newline to be "part of the end tag" then I must say that is really dumb.

It breaks all sorts of simple things like:

<pre>
- <?php echo("jack"); ?>
- <?php echo("jill"); ?>
</pre>

Now why on earth would PHP think it has the right to take away a newline character OUTSIDE of the end tag? In every other case the end tag finishes with the less-than symbol, no?

Now, if a newline is now part of the close tag, why not a space?

<?php echo('jack'); ?> <?php echo("jill"); ?>

Does the space now also get taken by PHP? Or is it outside the PHP blocks? And if it is outside, is it not because it is beyone the less-than symbol of the first block?
 [2007-05-04 11:26 UTC] mgf@php.net
The manual also states (at http://php.net/manual/en/language.basic-syntax.instruction-separation.php):

"The closing tag for the block will include the immediately trailing newline if one is present."

... and this has been the case forever.

However, I agree that it would probably be useful to modify the text you quoted to read something like:

"... when PHP hits the ?> closing tags, it simply starts outputting
whatever it finds (except for an immediately following newline) until it hits another opening tag."

... so I'm leaving this open as a Documentation Problem!
 [2007-05-04 11:39 UTC] fantasticjamieburns at hotmail dot com
Thanks for taking the time to look at this anyway.

For anyone else reading this in the future and thinking this is crazy behaviour this quickly gets around it for evaluated code:

$source = preg_replace('#<\?php (.*?) \?>\n#', "<?php \\1 ?>\n\n", $source);

$source = preg_replace('#<\?php (.*?) \?>\r\n#', "<?php \\1 ?>\r\n\r\n", $source);

:o)
 [2007-05-04 11:39 UTC] fantasticjamieburns at hotmail dot com
Ooops. closed.
 [2007-05-04 11:40 UTC] fantasticjamieburns at hotmail dot com
...
 [2007-05-04 12:41 UTC] rquadling@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.


 [2020-02-07 06:10 UTC] phpdocbot@php.net
Automatic comment on behalf of rquadling
Revision: http://git.php.net/?p=doc/en.git;a=commit;h=57c8a8955afbd07290c4ee77d8cc15da17a8cf0b
Log: Fix #41281 - include comment about newline being absorbed by &quot;end of php block&quot;.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Fri Sep 19 12:00:01 2025 UTC