How can the nl2br() function be effectively used in conjunction with PDO for displaying text with line breaks in HTML output?

When retrieving text from a database using PDO and displaying it in HTML, line breaks in the text may not be rendered correctly. To solve this issue, you can use the nl2br() function in conjunction with PDO to convert newline characters to HTML line breaks before displaying the text in the HTML output.

// Assume $pdo is your PDO database connection
$stmt = $pdo->prepare("SELECT text_column FROM your_table WHERE id = :id");
$stmt->bindParam(':id', $id);
$stmt->execute();
$row = $stmt->fetch();

$text_with_line_breaks = nl2br($row['text_column']);

echo $text_with_line_breaks;