What are potential pitfalls when formatting tooltips in PHP?

Potential pitfalls when formatting tooltips in PHP include not properly escaping special characters, not handling dynamic content securely, and not considering accessibility for users with disabilities. To avoid these pitfalls, it is important to sanitize user input, use htmlspecialchars() function to escape special characters, and provide alternative text for screen readers.

// Example of sanitizing user input and formatting tooltip in PHP
$tooltipContent = "<script>alert('XSS attack!');</script>"; // User input
$escapedTooltipContent = htmlspecialchars($tooltipContent); // Sanitize user input

echo "<a href='#' title='" . $escapedTooltipContent . "'>Hover over me</a>";