Quick Answer
htmlspecialchars() converts special HTML characters to their entities: & to &, < to <, > to >, " to ". Use before outputting user-supplied data in HTML to prevent XSS (Cross-Site Scripting) attacks. Always use: echo htmlspecialchars($userInput, ENT_QUOTES, "UTF-8"). htmlentities() converts more characters but htmlspecialchars() is usually sufficient.
Answer
htmlspecialchars() converts < > & characters into HTML entities to prevent XSS attacks.
S
SugharaIQ Editorial Team
Verified Answer
This answer has been peer-reviewed by industry experts holding senior engineering roles to ensure technical accuracy and relevance for modern interview standards.
PHP interview questions and answers for beginner, junior, mid-level, senior, and expert developers. Ideal for freshers and experienced candidates preparing for PHP roles.