What is the function nl2br() used for in PHP and how can it be implemented to preserve line breaks?
The nl2br() function in PHP is used to convert newline characters (\n) into HTML line breaks (<br>). This function is commonly used when displaying text that contains line breaks in an HTML document to ensure that the line breaks are preserved and displayed correctly.
$text = "Hello\nWorld";
echo nl2br($text);
Keywords
Related Questions
- What are the best practices for structuring PHP scripts to ensure that input validation, database updates, and output display are done in the correct order?
- What are some potential pitfalls when using PHP to automatically refresh a page after deleting an image without redirecting to another page?
- What are the advantages of using foreach loops over while loops when iterating through arrays in PHP?