What is the purpose of nl2br function in PHP and how is it commonly used?
The nl2br function in PHP is used to insert HTML line breaks (<br>) before all newlines in a string. This is commonly used when displaying text from a database or form input in an HTML document, as HTML does not recognize newline characters (\n) on its own.
// Example usage of nl2br function
$text = "Hello\nWorld!";
echo nl2br($text);
```
This code snippet will output:
```
Hello<br>World!
Related Questions
- What are common pitfalls in PHP development that can lead to issues like unwanted whitespace in the rendered output, and how can they be addressed proactively?
- What are some common pitfalls to watch out for when handling user input for database insertion in PHP?
- What is the recommended method to retrieve the last inserted ID in PHP when inserting into a database?