What are the differences between nl2br() and str_replace() when formatting text output in PHP?
When formatting text output in PHP, nl2br() is used to insert HTML line breaks (<br>) before all newlines in a string, while str_replace() is used to replace all occurrences of a specified value in a string with another value. The main difference is that nl2br() specifically targets newlines and adds HTML line breaks, while str_replace() can be used for a variety of replacements in a string.
// Using nl2br() to insert HTML line breaks before newlines in a string
$text = "Hello\nWorld";
echo nl2br($text);
// Using str_replace() to replace a specific value in a string
$text = "Hello, World!";
$new_text = str_replace("World", "PHP", $text);
echo $new_text;