PHP eats linefeed in php/plaintext mixed mode - php

This one is bothering me for a while. Let's say we have a simple PHP-File:
Line 0
Line 1
<?="Line 2"?>
Line 3
Processing this file will result in:
Line 0
Line 1
Line 2Line 3
Where did the line feed after ?> go? The linefeed is not beeing devoured when placing some character after the closing tag (e.g. ?>.).
Is there a way to control this behaviour? I'm not willing to place whitespaces after the closing tag, because my IDE is configured to remove whitespaces before linefeeds (and I like it that way).

Yes, indeed:
The closing tag for the block will include the immediately trailing newline if one is present.
http://php.net/manual/en/language.basic-syntax.instruction-separation.php
Meaning, if the ?> is the last thing on the line, the newline will be removed as part of the closing PHP block. You need to explicitly echo a newline or add an additional newline.

This is actually a feature (believe it or not). PHP consumes a linefeed if it directly follows a PHP close tag:
The closing tag for the block will include the immediately trailing
newline if one is present.
This was clearly put in so that a PHP file ending with a blank line would not cause a newline to occur in the output when included from another script. So it's really a "protect the ignorant" feature from the old days that we have to live with for the foreseeable future.
If you really want the newline there are other options: from simply putting in two newlines after the closing tag (the second will work!) to echoing a newline from code.

Outside of the <?php and ?> tags, the PHP interpreter operates in HTML mode and spacing inside HTML mode is less of an issue than it is for text contents.
To generate text with PHP you should use plain strings and build your output in this fashion:
$var = "Line 2";
$s = "Line 0\nLine 1\n$var\nLine3";
At least this won't give you a nasty, though documented, surprise :)

Related

Does omitting the closing tag of a PHP block at the end of a file remove any line feeds or whitespace,if present in a respective file?

I'm using PHP 7.1.12
I know that the closing tag of a block of PHP code automatically implies a semicolon so we do not need to have a semicolon terminating the last line of a PHP block.
The closing tag for the block will include the immediately trailing newline if one is present.
So, my question is suppose I'm having a PHP code block with few statements withing, I add ten line feeds after the last valid code line then I didn't add the closing PHP tag i.e. ?>
So, in above case will PHP remove those extra 10 line feeds or if any unwanted whitespace I've added?
If your files are pure PHP, i.e. you are not using multiple PHP blocks in an HTML file, you should always avoid closing tags.
Closing tags signify that your script is finished, and treat the next part of the code as HTML. if your HTML and PHP are separate in your codebase, you should start the file with <?php tag, and just write PHP script afterward. All the lines in that file will be treated as PHP, and only the part echoed back will be printed on the screen.
So coming back to your question, those 10 lines you've added after your last PHP-code line will be treated as part of PHP script, and will just be treated as empty lines in a script, instead of empty lines in HTML.
Some references for more information:
https://softwareengineering.stackexchange.com/questions/89553/closing-tag-on-php-files
https://www.sitepoint.com/should-you-close-your-php-code-tags/
https://wordpress.stackexchange.com/questions/210765/to-close-or-not-to-close-php
I know that the closing tag of a block of PHP code automatically
implies a semicolon so we do not need to have a semicolon terminating
the last line of a PHP block.
Means <?php echo '' ?> is fine.
The closing tag for the block will include the immediately trailing
newline if one is present.
Means
<?php echo '' ?>
\n
\n
\n
Will output 3 line breaks.
But I think your asking, whether the following will output line breaks.
<?php echo '';
\n
\n
\n
Which it wont, for the same reason PHP comments don't get outputted.
PSR-2 guidelines 2.2. Files.
All PHP files MUST use the Unix LF (linefeed) line ending.
All PHP files MUST end with a single blank line. (not 10 ;p)
The closing ?> tag MUST be omitted from files containing only PHP.

Which "trailing newline" the PHP manual is talking about? It's just confusing the manual readers and PHP learners. Please clear the confusion

I'm reading the PHP manual. I come across a sentence on the manual page(URL : Instruction separation). There is the first paragraph on this page saying :
As in C or Perl, PHP requires instructions to be terminated with a
semicolon at the end of each statement. The closing tag of a block of
PHP code automatically implies a semicolon; you do not need to have a
semicolon terminating the last line of a PHP block. The closing tag
for the block will include the immediately trailing newline if one is
present.
I understood everything written in the above paragraph except the last highlighted sentence i.e.
The closing tag for the block will include the immediately trailing
newline if one is present.
What does this sentence mean? What does this sentence imply?
This sentence has created confusion and doubts in my mind.
What exactly mean by "Trailing Newline" here?
Is the meaning of "Trailing Newline", "Line feed", HTML escape sequence character "\n", <br> same in this context? Or something else?
What exactly does the "trailing newline" mean here? The newline present just before the closing PHP tag (?>) or the newline present just after the closing PHP tag (?>)
?
Someone please answer all of my questions in simple, lucid and easy to understand language. It would be far better if you explain with some working code example demonstrating this concept.
Note : I've referred the already asked question on stackoverflow about this but its answer is not satisfactory, it's incomplete and vague. So, please don't mark my question as Duplicate of it. Thanks.
Basically, it says that if you have ?> in your code somewhere and then output on the next line, there won't be a newline/line feed/\n in the output there.
foo
<?php /* whatever */ ?>
bar
Will output
foo
bar
So even though there are three lines in the code, there are only two lines in the output.
Note that if you add a space after ?> on that second line, there will be a line with just a space between foo and bar. That is what is meant with the "immediately" part of the docs.
To answer your specific questions:
What exactly mean by "Trailing Newline" here?
It means the starting of a new line directly after the ?> closing tag.
Is the meaning of "Trailing Newline", "Line feed", HTML escape sequence character "\n", <br> same in this context? Or something else?
The "trailing newline" is the newline/line feed/\n that "trails" (comes directly after) the ?> closing tag. So newline, line feed and \n mean basically the same thing. It does not mean <br>.
What exactly does the "trailing newline" mean here? The newline present just before the closing PHP tag (?>) or the newline present just after the closing PHP tag (?>) ?
The newline present just after the closing tag ?>. This can be seen in my example, which has no newline before the ?>.
The closing tag for the block will include the immediately trailing newline if one is present.
I understood everything written in the above paragraph except the last highlighted sentence i.e.
What is meant by this is: if a closing tag is followed directly by a newline, the newline is considered to actually be a part of that closing tag.
So my example above is parsed as:
foo -> output "foo"
\n -> output a newline
<?php -> start processing PHP
/* whatever */
?>\n -> stop processing PHP
bar -> output "bar"

PHP is removing a newline typed after an echo statement

Consider test.php
OK
here
<?php echo "now"; ?>
what
if you were to run this file, you could expect it to output following:
OK
here
now
what
But it returns
OK
here
nowwhat
What is causing this? Can it be prevented?
P.S.
If you add any character after the line where php code is, even a space, then the newline is retiained.
OK
here
<?php echo "now"; ?>[space]
what
As in C or Perl, PHP requires instructions to be terminated with a semicolon at the end of each statement. The closing tag of a block of PHP code automatically implies a semicolon; you do not need to have a semicolon terminating the last line of a PHP block. The closing tag for the block will include the immediately trailing newline if one is present.
From php.net
So the "eating" of a newline is expected behavior. The work around as you found is to put a space, or use the above answer and add a \n.
Please add a break tag inside echo
OK
here
<?php echo "now <br>"; ?>
what
And your output will be like this:
OK
here
now
what

Is there a way to collapse whitespace before script open delimiter in php?

In php, if a closing script tag ?> is followed by whitespace up to and including a new line character, the whitespace is removed from the output.
This helps remove extra blank lines from the HTML. However the output still looks messy because the white space before the opening <?php delimiter is not collapsed.
Is there a way to control this behavior to either enable it for both delimiters or disable it altogether?

PHP omits a return

when a carriage return follows a closing php tag, php doesn't print it.
How can I change this?
Thanks a lot
That's normal behavior, and cannot be changed : the newline after a closing ?> is always ignored.
Here's the reference, in the FAQ of the PHP manual : Hey, what happened to my newlines?
(quoting, emphasis mine)
<pre>
<?php echo "This should be the first line."; ?>
<?php echo "This should show up after the new line above."; ?>
</pre>
In PHP, the ending for a block of code
is either "?>" or "?>\n" (where \n
means a newline). So in the example
above, the echoed sentences will be on
one line, because PHP omits the
newlines after the block ending. This
means that you need to insert an extra
newline after each block of PHP code
to make it print out one newline.
Why does PHP do this? Because
when formatting normal HTML, this
usually makes your life easier because
you don't want that newline, but you'd
have to create extremely long lines or
otherwise make the raw page source
unreadable to achieve that effect.
And here are a couple of interesting reads about this :
Rules pertaining to HTML or whitespace preceding or following PHP tags
PHP Stripping Newlines
The history of PHP eating newlines after the closing tag -- goes back to PHP 3 ^^
It's a default behavior of the language.
If you need the line break, you put a echo "\n" or echo "<br>" as the last line of the script.
This is intended behavior (see Escaping from HTML):
[…] when PHP hits the ?> closing tags, it simply starts outputting whatever it finds (except for an immediately following newline - see instruction separation ) […]

Categories