Line 3 ...">

PHP eats line feed in mixed php / plaintext mode

It bothers me a bit. 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 end after ?> ? Linear translation is not portable when placing any character after the closing tag (for example, ?>. ).

Is there a way to control this behavior? I don’t want to put spaces after the closing tag because my IDE is configured to remove spaces before the lines (and I like it).

+6
source share
3 answers

Yes indeed:

The final tag for the block will immediately include the ending new line, if present.

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

The value, if ?> Is the last thing in the line, the new line will be deleted as part of the closing PHP block. You need to explicitly specify echo new line or add an extra line to a new line.

+5
source

This one is actually a function (believe it or not). PHP consumes a string if it immediately follows the PHP close tag:

The closing tag for the block will include newline, if any.

This was explicitly done so that the PHP file ending with an empty line does not produce a new line in the output if include d from another script. Thus, it really “protects ignorance” from the old days with which we must live in the foreseeable future.

If you really want the new line to have other parameters: just insert two new lines after the closing tag (the second will work!) To repeat the new line from the code.

+4
source

Outside of the <?php and ?> Tags, the PHP interpreter operates in HTML mode, and the interval between HTML modes is less than for text content.

To generate text using PHP, you have to use simple lines and build your output like this:

 $var = "Line 2"; $s = "Line 0\nLine 1\n$var\nLine3"; 

At least it won’t give you any unpleasant, although documented , with surprise :)

+1
source

All Articles