What is the function of <textarea> in PHP and how does it handle line breaks?

The <textarea> element in PHP is used to create a multi-line text input field in HTML forms. When handling line breaks in a <textarea> field, PHP uses the PHP_EOL constant to represent the correct end-of-line character based on the operating system. To ensure proper handling of line breaks, we can use the nl2br() function in PHP to convert newlines to HTML line breaks (<br> tags) when displaying the text from the <textarea> field.

&lt;?php
if ($_SERVER[&quot;REQUEST_METHOD&quot;] == &quot;POST&quot;) {
    $text = $_POST[&#039;text&#039;];
    $text_with_linebreaks = nl2br($text);
    
    echo &quot;Text with line breaks: &quot; . $text_with_linebreaks;
}
?&gt;

&lt;form method=&quot;post&quot; action=&quot;&lt;?php echo htmlspecialchars($_SERVER[&quot;PHP_SELF&quot;]);?&gt;&quot;&gt;
    &lt;textarea name=&quot;text&quot;&gt;&lt;/textarea&gt;
    &lt;input type=&quot;submit&quot; value=&quot;Submit&quot;&gt;
&lt;/form&gt;