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 CONTENTSDOWNLOAD & NOTESFirstly, here is the download link to the example code as promised. QUICK NOTESIf 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 DOWNLOADClick 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 PHPAll right, let us now go through all the examples on how to add line breaks in PHP. 1) DEFINING LINE BREAKS AS IT IS1-as-it-is.php
The output
As 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 CHARACTERS2-characters.php
The output
What the heck is
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:
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 3) LINE BREAK USING PHP_EOL3-EOL.php
The output
Remember from 1 minute ago that we mentioned operating systems using either 4) IMPLODE LINE BREAKS4-implode.php
If you are dealing with multiple strings, it is a pain to manually add 5) NEWLINE TO HTML BREAK5-nl2br.php
If
you are working with HTML, the 6) HTML BREAK TO NEW LINE6-br2nl.php
Unfortunately, PHP does not offer the reverse function to convert HTML 7) WORD WRAP7-word-wrap.php
This
final useful bit is the
LINKS & REFERENCES
INFOGRAPHIC CHEAT SHEETLine Breaks in PHP (click to enlarge)THE ENDThank 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! How do I add a new line to a string in PHP?Using new line tags: Newline characters \n or \r\n can be used to create a new line inside the source code.
Does \n work in PHP?Answer: Use the Newline Characters ' \n ' or ' \r\n ' You can use the PHP newline characters \n or \r\n to create a new line inside the source code.
How do I add a new line to a string?In Windows, a new line is denoted using “\r\n”, sometimes called a Carriage Return and Line Feed, or CRLF. Adding a new line in Java is as simple as including “\n” , “\r”, or “\r\n” at the end of our string.
What does br /> mean in PHP?<br /> is an HTML element for a line break.
|