nl2br() Function in PHP 8.2, PHP 8.3 & PHP 8.4


The `nl2br()` function in PHP converts newline characters (`\n`, `\r\n`, or `\r`) to HTML line breaks (`<br>` or `<br />`). This is particularly useful when displaying user input or text files in HTML where you want to preserve line breaks.PHP 8.PHP 8.1,PHP 8.2,PHP 8.3 and PHP 8.4.

Basic Syntax
<?php
nl2br(string $string, bool $useXHTML = true): string
`$string`: The input string
`$useXHTML`: Whether to use XHTML compatible line breaks (`<br />`) or not (`<br>`). Default is `true`.
?>
Example 1: Basic Usage
<?php
$text = "Hello\nWorld\nThis is PHP";
echo nl2br($text);
?>
Output:
Hello<br />
World<br />
This is PHP

Important Notes for PHP 8.2
1. The function works the same in PHP 8.2 as in previous versions.
2. Always consider using `htmlspecialchars()` with user input to prevent XSS attacks.
3. The function handles all types of line breaks (`\n`, `\r\n`, and `\r`).
4. For better security, sanitize user input before using `nl2br()`.World<br />