4 Ways To Add Line Break & New Line In PHP

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:

  1. Define the linebreaks as-it-is in a string.
    • $lines = "Line 1
    • Line 2";
  2. Use the carriage return \r and newline \n characters – $lines = "Line 1\r\nLine 2";
  3. Use the PHP_EOL constant – $lines = "Line 1" . PHP_EOL . "Line 2";
  4. For HTML, use the <br> tag to add a new line – $lines = "Line 1<br>Line 2";

That covers the quick basics, but let us walk through more examples in this guide – Read on!

 

 

TABLE OF CONTENTS

 

DOWNLOAD & NOTES

Here is the download link to the example code, so you don’t have to copy-paste everything.

 

EXAMPLE CODE DOWNLOAD

Source code on GitHub Gist

Just click on “download zip” or do a git clone. I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

SORRY FOR THE ADS...

But someone has to pay the bills, and sponsors are paying for it. I insist on not turning Code Boxx into a "paid scripts" business, and I don't "block people with Adblock". Every little bit of support helps.

Buy Me A Coffee Code Boxx eBooks

 

 

CREATING LINE BREAKS IN PHP

All right, let us now go through all the examples of 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 Line

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 CHARACTERS

2-characters.php
<?php
// LINE BREAK CHARACTERS
// LINUX USES \N, MAC USES \R, WINDOWS USES \R\N
// SAFEST BET IS TO USE \R\N
echo "Line 1\r\nLine 2\r\n";

// ESCAPE CHARACTERS WILL ONLY WORK WITH DOUBLE QUOTED STRINGS!
echo 'Line 1\r\nLine 2';
The output
D:\http>php 2-characters.php
Line 1
Line 2
Line 1\r\nLine 2

What 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 Doe

Remember 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.

 

EXTRAS

That’s all for this tutorial, and here is a small section on some extras that may be useful to you.

 

LINKS & REFERENCES

 

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!

Leave a Comment

Your email address will not be published. Required fields are marked *