How to create a new line in PHP
In this tutorial, we will learn How to create a new line in PHP. You can use the PHP newline character \n or \r\n to create a new line. If you want the line breaks to be in the browser. use can use The nl2br()
is an inbuilt function in PHP and is used to insert HTML break tags(<br> or <br />)
in the place of all new lines(\n) in a string.
For normal text editors, the new line is generally denoted by using any of the following.
\n
\r
\n\r
\r\n
\n
: The cursor is to be moved to the next line.\r
: The cursor to be moved to the beginning of the line. \r
takes strings that may contain newlines and returns an altered string by inserting <br>
tag.
Use the newline characters ‘\n’ or ‘\r\n’
Example
<?php
$html = "Hey Devnote! \n How are you?";
echo nl2br($html);
Output
Hey Devnote!
How are you?
Notes: Different Operating systems prefer to use a different character as line breaks such as Windows uses \r\n, Linux uses \n and MAC uses \r.