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!

Did you enjoy this post?

Cool if I slip into your inbox with more?
Full posts, 1-2 times per week: