What potential pitfalls should be avoided when generating HTML elements within a PHP function?

When generating HTML elements within a PHP function, it's important to avoid mixing PHP and HTML code together, as it can make the code harder to read and maintain. Instead, use PHP to generate the dynamic content and output it within the HTML structure. Additionally, be cautious of potential security vulnerabilities such as cross-site scripting (XSS) attacks by properly sanitizing user input before outputting it as HTML.

<?php
// Example of generating HTML elements within a PHP function without mixing PHP and HTML code
function generateElement($content) {
    $sanitizedContent = htmlspecialchars($content); // Sanitize user input
    $html = '<div class="element">' . $sanitizedContent . '</div>';
    return $html;
}

// Usage
$content = "Hello, world!";
echo generateElement($content);
?>