New Line in PHP CLI

Steve Popoola —  May 29, 2012 — 1 Comment

For many new to PHP and even for experienced once, sometimes you experience some weird behaviour and wonder why it happens? It is usually because we have not taken time to read the PHP documentation to know how specific command work and some of the behaviour we should expect of not used properly.

Once of such is the echo command. The echo command prints out to the console or page, the string delimited by single or double quotes.
for exampple;
echo 'This is a string'

Will output, This is a string.

It is usual for developers to use Escape sequences \n for linux and \r\n for Windows to break the string into lines.

The problem arises when you use single quotes to wrap these characters.
Take for example;

echo 'This is line 1 \n This is line 2';

This will output the unexpected result, This is line 1\nThis is line 2

The reason is that Escape sequences are only parsed when they are inside double quotes.

The above will work as follows;

echo "This is line 1 \n This is line 2";

This is line 1
This is line 2

Don’t forget to read the PHP Manual!

Steve Popoola

Posts Google+

One response to New Line in PHP CLI

  1. I intended to write you this tiny oriobvatsen just to say thanks again on your remarkable knowledge you have discussed above. It was simply pretty generous of you to deliver extensively exactly what a lot of folks could have advertised as an ebook to help with making some bucks on their own, particularly since you could possibly have tried it if you ever considered necessary. These secrets additionally worked to be the good way to understand that other people online have the same keenness similar to mine to realize a little more with regard to this problem. I know there are lots of more enjoyable periods in the future for people who read through your website.

Leave a Reply


Text formatting is available via select HTML.

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>