Jason Posit
Jason Posit

Reputation: 1323

PHP tags deleting trailing newline

When I run the following PHP code from the command line:

$ php test.php

where the contents of test.php are as follows

Foo
<? echo "Hello World" ?>
Bar

I am getting as output:

Foo
Hello WorldBar

instead of:

Foo
Hello World
Bar

which is what I could swear previous versions of PHP would output. It is though the closing php tag now consumes the following newline character which wasn't happening in earlier versions of PHP as far as I can remember. Is there a setting somewhere in some php.ini or other configuration file which toggles this or similar whitespace munching behavior?

Thanks.

Upvotes: 0

Views: 79

Answers (2)

Hayk Aramyan
Hayk Aramyan

Reputation: 323

You need to put \n in the end of your string in echo

try this

Foo
<? echo "Hello World\n" ?>
Bar

Upvotes: 1

cmt
cmt

Reputation: 1485

Foo
<? echo "Hello World" ?> 
Bar

(there is 1 space after the ?>, e.g. as a string it is "?> ") will output:

Foo
Hello World
Bar

This behavior is specified in the PHP manual (http://www.php.net/manual/en/language.basic-syntax.phpmode.php) as:

when the PHP interpreter hits the ?> closing tags, it simply starts outputting whatever it finds (except for an immediately following newline [...])

Now, to answer your question "Is there a setting somewhere in some php.ini or other configuration file which toggles this or similar whitespace munching behavior?"

No there isn't, according to the link/bug report provided by Orangepill (bugs.php.net/bug.php?id=21891). In the bug report, it is stated that there will never be an option to control this behavior because it makes writing portable scripts harder.

Upvotes: 2

Related Questions