Friday, October 10, 2025
HomeLanguagesHow to insert a line break in PHP string ?

How to insert a line break in PHP string ?

In this article, we will discuss how to insert a line break in a PHP string. We will get it by using the nl2br() function. This function is used to give a new line break wherever ā€˜\n’ is placed.

Syntax:

nl2br("string \n");

where the string is the input string.

Example 1: PHP Program to insert a line break in a string.

PHP




<?php
Ā 
// Insert a new line break
echo nl2br("This is first line \n This is second line");
Ā 
?>


Output

This is first line <br />
 This is second line

Example 2:

PHP




<?php
Ā 
// Insert a line break with new line
echo nl2br("This is first line \n This is second "
Ā Ā Ā + "line \n This is third line \n This is fourth line");
Ā 
?>


Output

This is first line <br />
 This is second line <br />
 This is third line <br />
 This is fourth line
RELATED ARTICLES

Most Popular

Dominic
32349 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6717 POSTS0 COMMENTS
Nicole Veronica
11880 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6838 POSTS0 COMMENTS
Ted Musemwa
7097 POSTS0 COMMENTS
Thapelo Manthata
6792 POSTS0 COMMENTS
Umr Jansen
6792 POSTS0 COMMENTS