Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to insert a line break in PHP string ?

  • Last Updated : 07 Oct, 2021

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

Syntax:

nl2br("string \n");

where, 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 forth line");
  
?>
Output
This is first line <br />
 This is second line <br />
 This is third line <br />
 This is forth line
My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!