PHP阻止后缺少新行

When I processed the following file src.php:

<?php
include "params.php";
?>
Str 1
val a = <?=$b?>
Str 3

where the params.php file:

$b = 123;

$ php src.php > tgt.txt

I get the result:

Str 1
val a = 123Str 3

instead of

Str 1
val a = 123
Str 3

How shoud I solve this problem? Should I add an additional line break after <?=b?> through all my code?

This is an issue I've come across before with the way PHP blocks are processed, the first line-break after ?> is, as far as I know, always ignored. This is probably[citation needed] to avoid problems when using 'code only' PHP files which would output a trailing newline when include'd, e.g.:

-- some_file.php
<?php
// some class/function definitions, etc.
?>
+ trailing newline

-- some_other_file.php
include 'some_file.php'; // this would actually output a linebreak

As a lot of UNIX editors enjoy messing around with your whitespace this was probably seen as a necessary 'feature' in order to avoid this issue.

TL;DR:

The only workaround I'm aware of is to either concatenate a linebreak to your output

<?= $b.PHP_EOL ?>

or add another linebreak in the HTML

val a = <?= $b ?>

Str 3

Both of these solutions suck, but I don't know any other way around.

EDIT: A different approach would be to use heredoc strings:

echo <<<STUFF
Str 1
val a = $b
Str 3
STUFF;
// must be a break after `STUFF;`

That way formatting is still clear, and it behaves as you'd expect it (though a caveat with heredocs is there must be a linebreak after the terminating STUFF;, or you'll get a parse error, at least on 5.4).

UPDATE thanks to adam:

try

$b = "123".PHP_EOL;

anyway seems like you need to add a new line :)

Why don't you try:

val a = <?=$b."
";?>
Str 3

Or just:

val a = <?=$b;?><br/>
Str 3