0

Consider the following PHP code:

<?php
echo "prefix";
echo "
{{ foo }}
"; 
echo "suffix";

It renders as:

prefix
{{ foo }}
suffix

As can be seen here:

http://sandbox.onlinephpfunctions.com/code/4c0b3a3e921b0a761b1cf05f12f709d025d1b2de

Note the line feed between prefix and {{ foo }}.

Now consider the following code:

<?php
echo "
{{ foo }}
"; 
echo "suffix";

It renders as:

{{ foo }}
suffix

As can be seen here:

http://sandbox.onlinephpfunctions.com/code/18b07b9d05b79703617364b4d301d6799654a086

Why was the first line feed not rendered in the second case while it was in the first?

Eric MORAND
  • 6,373
  • 4
  • 27
  • 34

1 Answers1

0

This seems to be because the web browser (or website running the PHP for you) is removing the extra whitespace from the rendered HTML output. (See: https://stackoverflow.com/a/17784740)

If you run your code on the PHP command line, then you will see the leading newline that you desire.

If this code is going to be used in a HTML page, then use <br/> tags to make new lines.

gen_Eric
  • 223,194
  • 41
  • 299
  • 337