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! What is a line break in PHP?You can use the PHP newline characters \n or \r\n to create a new line inside the source code. However, if you want the line breaks to be visible in the browser too, you can use the PHP nl2br() function which inserts HTML line breaks before all newlines in a string.
Which is the correct way to end a PHP statement?Note: PHP statements end with a semicolon ( ; ).
Can we use \n in PHP?It is an in-built function of PHP, which is used to insert the HTML line breaks before all newlines in the string. Although, we can also use PHP newline character \n or \r\n inside the source code to create the newline, but these line breaks will not be visible on the browser.
Which tag is used on line break in PHP?Syntax: nl2br("string \n"); where, string is the input string. Example 1: PHP Program to insert a line break in a string.
|