PHP nl2br() Function

AuthorSumit Dey Sarkar

Pubish Date18 Mar 2023

categoryPHP

In this article we will learn about PHP nl2br function.

It is a predefine function which use to insert HTML line break before all new lines. This function replace \n to <br> or </br> (This function replace \n into new line like HTML br).

Let's see a example -

 

PHP nl2br function

This PHP nl2br function will take two arguments.

nl2br(string,xhtml)

1) First as string (Required)

2) Boolean value, whether to use XHTML compatible line breaks or not. .True - Default. Inserts <br /> .False -Insert <br> (Optional)

 

<?php
$myVar = "This is line one \n This is line two \n This is line three \n This is line four \n This is line five ";
echo nl2br($myVar);
?>

 

Output

This is line one

This is line two

This is line three

This is line four

This is line five

Comments 0

Leave a comment