Step 1: Understand what htmlspecialchars does to special characters
The characters <, >, and & are converted to <, >, and & respectively.
Step 2: Apply htmlspecialchars to the input string
The string 'Hello & Welcome' becomes '<b>Hello & Welcome</b>'.
Final Answer:
<b>Hello & Welcome</b> -> Option D
Quick Check:
Special chars escaped = <b>Hello & Welcome</b> [OK]
Hint: Remember: <, >, & become <, >, & [OK]
Common Mistakes:
Expecting HTML tags to remain unescaped
Not escaping ampersand (&)
Confusing output with raw HTML
4. Identify the error in this PHP code snippet:
$text = '';
echo htmlspecialchars $text;
medium
A. Incorrect variable name
B. Missing parentheses when calling htmlspecialchars
C. htmlspecialchars does not escape quotes
D. Missing semicolon after variable assignment
Solution
Step 1: Check function call syntax
Functions in PHP require parentheses around arguments. Here, htmlspecialchars $text; is missing parentheses.
Step 2: Confirm other syntax elements
Variable name and semicolons are correct; quotes are escaped by default.
Final Answer:
Missing parentheses when calling htmlspecialchars -> Option B
Quick Check:
Function calls need parentheses = Missing parentheses when calling htmlspecialchars [OK]
Hint: Always use parentheses when calling functions [OK]
Common Mistakes:
Calling functions without parentheses
Assuming quotes are not escaped
Overlooking semicolon placement
5. You want to safely display user input that may contain HTML tags and quotes. Which htmlspecialchars call correctly escapes all special characters including double and single quotes?
hard
A. htmlspecialchars($input, ENT_QUOTES | ENT_HTML5)
B. htmlspecialchars($input, ENT_NOQUOTES)
C. htmlspecialchars($input, ENT_COMPAT)
D. htmlspecialchars($input)
Solution
Step 1: Understand the flags for htmlspecialchars
ENT_QUOTES escapes both single and double quotes; ENT_HTML5 ensures HTML5 entities.
Step 2: Compare options
htmlspecialchars($input, ENT_QUOTES | ENT_HTML5) uses ENT_QUOTES and ENT_HTML5, escaping all quotes and special chars safely.
Final Answer:
htmlspecialchars($input, ENT_QUOTES | ENT_HTML5) -> Option A
Quick Check:
Use ENT_QUOTES to escape all quotes = htmlspecialchars($input, ENT_QUOTES | ENT_HTML5) [OK]
Hint: Use ENT_QUOTES flag to escape all quotes [OK]
Common Mistakes:
Using ENT_NOQUOTES which skips quote escaping
Assuming default escapes single quotes
Not specifying ENT_HTML5 for modern HTML
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.