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. Born again Linux user. Founder of Holiday API, Backend Engineering Manager and Emoji Specialist at Mailshake (WE'RE HIRING!), and author of the best damn Lorem Ipsum Library for PHP.

Currently Reading

Pears and Perils
Antifragile: Things That Gain from Disorder
Code Girls: The Untold Story of the American Women Code Breakers of World War II

Previous Reads

Buy Me a Coffee Become a Sponsor

Related Articles