New Lines in Command-line PHP Output

Josh Sherman
1 min read
Software Development PHP

If you’ve ever worked with command-line PHP and outputted text you know that PHP takes absolutely no liberties with injecting line breaks into the output. There’s also no magic function or configuration variable that will adjust this behavior. Easy enough, you want line breaks, just add them!

echo "I am a line\n";
echo "I am the next line\n\n";
echo "I have a blank line above me\n";
echo 'Hello\n';
echo 'World\n';

The first three lines are working examples for inserting new lines at the end of your output. The last 2 lines will result in output like this Hello\nWorld\n. Why is that? Because single quotes in PHP display the literal string whereas double quotes will interpret the escaped character as you’d expect it. This also comes in handy when inserting variables into a string!

Join the Conversation

Good stuff? Want more?

Weekly emails about technology, development, and sometimes sauerkraut.

100% Fresh, Grade A Content, Never Spam.

About Josh

Husband. Father. Pug dad. Musician. Founder of Holiday API, Head of Engineering and Emoji Specialist at Mailshake, and author of the best damn Lorem Ipsum Library for PHP.

Currently Reading

Parasie Eve

Previous Reads

Buy Me a Coffee Become a Sponsor

Related Articles