Welcome to a quick tutorial on how to add line breaks and newlines in PHP. Just started with PHP and wondering how to break a string into multiple lines in PHP? The common ways to add line breaks and newlines in PHP are:
That covers the quick basics, but let us walk through more examples in this guide – Read on!
ⓘ I have included a zip file with all the source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.
QUICK SLIDES
TABLE OF CONTENTS
DOWNLOAD & NOTES
Firstly, here is the download link to the example code as promised.
QUICK NOTES
If you spot a bug, feel free to comment below. I try to answer short questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.
EXAMPLE CODE DOWNLOAD
Click here to download the source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.
CREATING LINE BREAKS IN PHP
All right, let us now go through all the examples on how to add line breaks in PHP.
1) DEFINING LINE BREAKS AS IT IS
1-as-it-is.php
<?php echo "Hello World Foo Bar!"; $aString = " First Line Second Line Third Line"; echo $aString;The output
D:\http>php 1-as-it-is.php Hello World Foo Bar! First Line Second Line Third LineAs you can see, PHP will register the line breaks as-it-is. We actually don’t need to use any “code gimmicks” to add new lines to strings.
2) LINE BREAK & NEWLINE CHARACTERS
2-characters.php
The output
D:\http>php 2-characters.php Line 1 Line 2 Line 1\r\nLine 2What the heck is \r\n and why do so many people recommend using it? In layman terms, these are “special characters”:
- \r Represents a carriage return (hex code 0D)
- \n Represents a newline (hex code 0A)
As to why there are 2 different characters – Welcome to the confusing cyber world, where everyone uses a different standard to represent a line break:
- Unix systems (Linux) use \n
- Mac systems use \r
- Windows use \r\n
Yep, this confusion really isn’t PHP’s fault. For us code ninjas, just remember that the safest for cross-platform compatibility is to use \r\n regardless.
3) LINE BREAK USING PHP_EOL
3-EOL.php
<?php $people = [ "John Doe", "Jane Doe", "June Doe", "Joy Doe", "Julius Doe" ]; foreach ($people as $person) { echo $person . PHP_EOL; }The output
D:\http>php 3-EOL.php John Doe Jane Doe June Doe Joy Doe Julius DoeRemember from 1 minute ago that we mentioned operating systems using either \r, \n, or \r\n? The PHP_EOL constant is a convenience that PHP provides – It will automatically use the correct line break character for you.
4) IMPLODE LINE BREAKS
4-implode.php
<?php $people = [ "John Doe", "Jane Doe", "June Doe", "Joy Doe", "Julius Doe" ]; echo implode("\r\n", $people); echo implode(PHP_EOL, $people); echo implode("<br>", $people);If you are dealing with multiple strings, it is a pain to manually add \r\n to each and every line. This is a common trick, use implode() to quickly combine the strings while adding line breaks in between.
5) NEWLINE TO HTML BREAK
5-nl2br.php
<!DOCTYPE html> <html> <body><?php $text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed a imperdiet sem. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras pulvinar porttitor ligula, et molestie quam luctus convallis."; echo nl2br($text); ?></body> </html>If you are working with HTML, the nl2br() function will save you a lot of time by automatically converting the line breaks into <br> tags.
6) HTML BREAK TO NEW LINE
6-br2nl.php
<?php $text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.<br>Sed a imperdiet sem. Lorem ipsum dolor sit amet, consectetur adipiscing elit.<br>Cras pulvinar porttitor ligula, et molestie quam luctus convallis."; echo str_replace("<br>", PHP_EOL, $text);Unfortunately, PHP does not offer the reverse function to convert HTML <br> tags into line breaks. But thankfully, the “reverse” is as easy as doing a string replacement.
7) WORD WRAP
7-word-wrap.php
<?php $text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed a imperdiet sem. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras pulvinar porttitor ligula, et molestie quam luctus convallis."; echo wordwrap($text, 50, PHP_EOL, true); echo wordwrap($text, 50, "<br>", true);This final useful bit is the wordwrap (STRING, WIDTH, BREAK, CUT) function. As you might have already guessed, we use this one to split a chunk of text into lines quickly.
- STRING The string that we want to work with.
- WIDTH The maximum number of characters per line.
- BREAK Separate lines using this character.
- CUT True or false. If true, words will be cut into half to make sure that it meets the number of characters per line specified in WIDTH.
LINKS & REFERENCES
- PHP escape characters
- Reserved constants in PHP
- PHP nl2br
- Word wrap
INFOGRAPHIC CHEAT SHEET
THE END
Thank you for reading, and we have come to the end of this guide. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!