How to create a new line in PHP
Topic: PHP / MySQLPrev|Next
Answer: Use the Newline Characters '
\n' or '
You can use the PHP newline characters
\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.
Let's take a look at the following example to understand how it basically works:
ExampleTry this code »
<?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
Here are some more FAQ related to this topic:
- How to combine two strings in PHP
- How to remove white space from a string in PHP
- How to write comments in PHP