Adding line break using PHP

August 3, 2010

For the formatting purpose we need to add line breaks in our output or strings to store in MySQL tables and all. Here I am describing about various methods to add line break using PHP.

In HTML we can add a line break using <br> or <br /> tag.
For this purpose in PHP we need to simply echo that tag like following code

<?php
echo "syam is here<br>for teaching you PHP";
?>

Its output will be
syam is here
for teaching you PHP
( We can see that a line break is added after “syam is here”)

But in several other cases line break is applying using “\n” or “\r\n”, like data storage in csv , txt files or mysql tables or data in a text area etc. In that cases we need to use function nl2br()
to convert the “\n” into “<br>” tags to display line break.

See the sample PHP program for display line break using nl2br()

<?php
echo nl2br("syam is here\nfor teaching you PHP");
?>

Its output also will be same as our previous PHP program
syam is here
for teaching you PHP
( we can see that “\n” is decoded to <br> tag by nl2br() )

There is PHP_EOL constant available instead of using “\n: or “\r\n”.

See the following sample PHP program with constant PHP_EOL

<?php
echo nl2br("syam is here".PHP_EOL."for teaching you PHP");
?>

Its output also will be same as our previous PHP programs
syam is here
for teaching you PHP
( here we can see that constant PHP_EOL is converted into line break using nl2br )

Most of the time we can use “<br>” tag itself for displaying the line breaks in our HTML contents or displaying areas. The other things will come under some advanced cases that we can understand in our later postings.

Related Tutorials


Break in PHP
“break” statement is using for end the current execution of loop control structure. Its applicable for for, foreach, while, do-while...
PHP Switch case
PHP Switch case statement is an alternative for complex IF statements with a same expression. Its a best alternative for...
Concatenate strings in PHP using concatenation operator
Concatenation is the process of joining multiple strings into one.In PHP concatenate  strings is doing by concatenation operator ( “.”...
PHP continue statement
PHP “continue” statement is used within the loops to skip the current loop iteration and goes to the next iteration....
PHP Value Assignment Operators
Here we can learn about the operators used for assigning values in PHP. Operator “=” Usage Sample program <?php $x...
PHP trim() – Remove whitespace from string
PHP trim() function is used for removing the white space or other characters from starting and end of a string....
PHP fread() – reading a file
PHP fread() function is used for reading contents of a file. It read from the file stream pointed to by...
Array in PHP tutorial
Array is a special variable which can store multiple value. ( Note : a normal variable can store only one...

Tags: , ,

One Response to Adding line break using PHP

  1. Tuttu on August 3, 2010 at 1:00 pm

    Hey, I was searching for this topic. Now got the solution. My issue was I am using \n without nl2br function and expecting a line break. But it simply display \n itself. Now it starts to show line break . Thanks

Leave a Reply