What are some common pitfalls when using templates in PHP?

One common pitfall when using templates in PHP is not properly escaping user input, which can lead to security vulnerabilities such as cross-site scripting (XSS) attacks. To solve this issue, always use functions like htmlspecialchars() to escape user input before outputting it in your templates.

<?php
// Example of properly escaping user input in a template
$userInput = "<script>alert('XSS attack');</script>";
?>
<div><?php echo htmlspecialchars($userInput); ?></div>