How to create a new line in PHP

Topic: PHP / MySQL« Prev|Next »

Answer: Use the Newline Characters '\n' or '\r\n'

You can use the PHP newline characters \n or \r\n to create a new line inside the source code. However, if you want the line breaks to be visible in the browser too, you can use the PHP nl2br() function which inserts HTML line breaks before all newlines in a string.

  • <?php
  • echo "If you view the page source \r\n you will find a newline in this string.";
  • echo "<br>";
  • echo nl2br("You will find the \n newlines in this string \r\n on the browser window.");
  • ?>
 

Note:The character \n writes a newline in UNIX while for Windows there is the two character sequence: \r\n. To be on safe side use the \r\n instead.


Related FAQ

Here are some more FAQ related to this topic:

 
Close

Your Feedback:

 

We would love to hear from you! Please say something.