Skip to content
PHP Logo

What are the best practices for handling line breaks and formatting in PHP when displaying text in a textarea?

When displaying text in a textarea in PHP, it's important to handle line breaks properly to maintain the formatting of the text. To do this, you can use the PHP function nl2br() to convert newline characters to HTML line breaks. This will ensure that the text is displayed correctly with line breaks in the textarea.

php
<?php
$text = "This is a sample text with\nline breaks.";
echo nl2br($text);
?>