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 wheninclude‘d, e.g.: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
or add another linebreak in the HTML
Both of these solutions suck, but I don’t know any other way around.
EDIT: A different approach would be to use heredoc strings:
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).