Example 1 — Add Line Breaks
Turn plain text new lines into HTML breaks.
<?php
$text = "Line 1\nLine 2";
echo nl2br($text), PHP_EOL; How It Works
- Build a two-line string. The \n splits the lines.
- Call nl2br. A br tag is added before the newline.
- The newline stays. The text still wraps in the page source.
- Browsers read the tag. In a web page you would see two lines.

