What are common pitfalls when handling special characters in PHP strings?

Common pitfalls when handling special characters in PHP strings include not properly escaping or sanitizing user input, which can lead to security vulnerabilities such as SQL injection or cross-site scripting attacks. To avoid these issues, always use functions like `htmlspecialchars()` or `mysqli_real_escape_string()` to sanitize user input before using it in your application.

// Example of properly escaping special characters in PHP strings
$user_input = "<script>alert('XSS attack!')</script>";
$escaped_input = htmlspecialchars($user_input, ENT_QUOTES, 'UTF-8');
echo $escaped_input;