What are the potential pitfalls of using PHP to generate and display dynamic content on a webpage?

One potential pitfall of using PHP to generate and display dynamic content on a webpage is the risk of cross-site scripting (XSS) attacks if user input is not properly sanitized. To prevent this, always sanitize and validate user input before displaying it on the webpage.

// Sanitize user input before displaying on the webpage
$userInput = "<script>alert('XSS attack!');</script>";
$sanitizedInput = htmlspecialchars($userInput, ENT_QUOTES, 'UTF-8');
echo $sanitizedInput;