Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Line Break in PHP – A Beginner’s Guide

INTRODUCTION
GAME OF NEW LINES

Welcome to a beginner’s tutorial on how to add a line break in PHP. Quick answer – We can use \r\n to add a new line to a string. But there is actually more than meets the eye. Why are there 2 of them? What do those characters mean? Find out in this guide, and we will also walk through some neat tricks with new lines – Read on!

I have included a zip file with all the source code at the end of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

CONFESSION
AN HONEST DISCLOSURE

Quick, hide your wallets! I am an affiliate partner of Google, eBay, Adobe, Bluehost, Clickbank, and more. There are affiliate links and advertisements throughout this website. Whenever you buy things from the evil links that I recommend, I will make a commission. Nah. These are just things to keep the blog going, and allows me to give more good stuff to you guys - for free. So thank you if you decide to pick up my recommendations!


 

NAVIGATION
TABLE OF CONTENTS

Section A
The Basics

Section B
More Line Yoga

Extra
Useful Bits

Extra
Source Code Download

Closing
What’s Next?

SECTION A
THE BASICS

In this first section, let us get started with the basics of line breaks (new lines) in PHP.

LINE BREAKS – AS IT IS

1-new-line.php
The output
D:\http\test>php 1-new-line.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.

LINE BREAKS – ESCAPE CHARACTERS

2-escape-character.php
The output
D:\http\test>php 2-escape-characters.php
Hello
World
Foo Bar!
Hello\r\nWorld\r\nFoo Bar!

So what the heck is \r\n and why do so many people recommend using it? In the layman terms, these are special characters known as “escape characters”, more specifically:

  • \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) uses \n
  • Mac systems uses \r
  • Windows uses \r\n

Yep, this confusion really isn’t PHP’s fault. But for us code ninjas, just remember – The safest for cross-platform compatibility is to stick to using \r\n.

WHY USE ESCAPE CHARACTERS?

If PHP will register line breaks as-they-are, why do we even need to go through so many pains to use the confusing \r\n escape characters? I can personally think of 3 reasons:

  • Some people just love to torture themselves.
  • Code wise, to keep the strings in a “flat single line”, easier to understand.
  • Used when looping through an array (example below).
3-array-line.php
The output
D:\http\test>php 3-array-line.php
John Doe
Jane Doe
June Doe
Joy Doe
Julius Doe

PHP END OF LINE

Remember from 1 minute ago that we mentioned every operating system uses a different character to represent line break? \r, \n, or \r\n? This is an alternative, and that is to use the PHP_EOL constant – Where PHP will automatically choose the correct line break character for you.

4-EOL.php
The output
D:\http\test>php 4-EOL.php
John Doe
Jane Doe
June Doe
Joy Doe
Julius Doe

SECTION B
MORE LINE YOGA

Now that you have mastered the art of new lines on PHP, here are a few more related functions that are good to know.

NEW LINE TO BREAK

If you are working with HTML, the nl2br function will save you a lot of time by automatically converting the line breaks into
tags.

5-nl2br.php
= $text ?>
  

BREAK TO NEW LINE

Unfortunately, PHP does not offer the reverse function to convert HTML
tags into line breaks, so we will have to manually string replace it:

6-br2nl.php
Sed a imperdiet sem. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Cras pulvinar porttitor ligula, et molestie quam luctus convallis."; $text = str_replace("
", PHP_EOL, $text); echo $text; ?>

WORD WRAP

This final useful bit is the wordwrap (STRING, WIDTH, BREAK, CUT) function. As you might have already guessed it, we use this one to split a chunk of text into lines quickly.

  • STRING The string that we want to work with.
  • WIDTH 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.
7-word-wrap.php
", true);
?>

EXTRA
USEFUL BITS

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

REFERENCES

  • PHP escape characters
  • Reserved constants in PHP
  • PHP nl2br
  • Word wrap

INFOGRAPHIC CHEAT SHEET

Line Breaks in PHP (click to enlarge)

EXTRA
DOWNLOAD

Finally, here is the download link to all the example source code as promised.

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

CLOSING
WHAT’S NEXT?

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!

The post Line Break in PHP – A Beginner’s Guide appeared first on Code Boxx.



This post first appeared on Xxxxxxxxx, please read the originial post: here

Share the post

Line Break in PHP – A Beginner’s Guide

×

Subscribe to Xxxxxxxxx

Get updates delivered right to your inbox!

Thank you for your subscription

×