New Lines in Command-line PHP Output

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 linen"
echo "I am the next linenn"
echo "I have a blank line above men"
echo 'Hellon'
echo 'Worldn'

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 HellonWorldn. 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!

Josh Sherman - The Man, The Myth, The Avatar

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.

If you found this article helpful, please consider buying me a coffee.