What is the purpose of the wordwrap() function in PHP and how is it used in the context of text formatting?

The wordwrap() function in PHP is used to wrap a string to a given number of characters. This is useful for formatting text to fit within a certain width, especially when displaying text in a fixed-width container like a terminal or a webpage. By specifying the desired line length, the wordwrap() function breaks the string into multiple lines without breaking words.

$text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";
$wrapped_text = wordwrap($text, 30, "<br>");

echo $wrapped_text;