Recall & Review
beginner
What is the purpose of the
htmlspecialchars function in PHP?It converts special characters to HTML entities to prevent code injection and display text safely in HTML.
Click to reveal answer
beginner
Which characters does
htmlspecialchars convert by default?It converts & (ampersand), < (less than), > (greater than), and " (double quote).
Click to reveal answer
intermediate
What is the difference between
htmlspecialchars and htmlentities?htmlspecialchars converts only special HTML characters, while htmlentities converts all applicable characters to HTML entities.Click to reveal answer
beginner
How do you use
htmlspecialchars to safely display user input in HTML?Wrap the user input with
htmlspecialchars($input, ENT_QUOTES, 'UTF-8') before outputting it.Click to reveal answer
intermediate
Why is it important to specify the character encoding like
'UTF-8' in htmlspecialchars?Specifying encoding ensures correct conversion of characters and prevents security issues related to character misinterpretation.
Click to reveal answer
What does
htmlspecialchars('<script>alert(1)</script>') output?✗ Incorrect
It converts < and > to < and > so the tags show as text, not HTML.
Which flag makes
htmlspecialchars convert single quotes as well?✗ Incorrect
ENT_QUOTES converts both double and single quotes to HTML entities.
Why should you escape output with
htmlspecialchars when displaying user input?✗ Incorrect
Escaping prevents users from injecting harmful HTML or scripts.
What happens if you don't specify the encoding in
htmlspecialchars?✗ Incorrect
Without specifying encoding, some characters may be misinterpreted, causing security issues.
Which of these is NOT converted by default by
htmlspecialchars?✗ Incorrect
The apostrophe (') is not converted by default unless ENT_QUOTES is used.
Explain why and how you use
htmlspecialchars when showing user input on a webpage.Think about keeping the webpage safe from harmful code.
You got /4 concepts.
Describe the difference between
htmlspecialchars and htmlentities.One converts fewer characters, the other more.
You got /4 concepts.