0
0
PHPprogramming~20 mins

Output escaping with htmlspecialchars in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
HTML Specialchars Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of htmlspecialchars with ENT_QUOTES?
Consider the following PHP code snippet. What will it output?
PHP
<?php
$input = "Tom & Jerry's <show>";
echo htmlspecialchars($input, ENT_QUOTES);
?>
ATom &amp; Jerry&apos;s &lt;show&gt;
BTom &amp; Jerry's &lt;show&gt;
CTom & Jerry's <show>
DTom &amp; Jerry&#039;s &lt;show&gt;
Attempts:
2 left
💡 Hint
Remember ENT_QUOTES converts both double and single quotes.
Predict Output
intermediate
2:00remaining
Output of htmlspecialchars without flags parameter
What will this PHP code output?
PHP
<?php
$text = '5 > 3 & 2 < 4';
echo htmlspecialchars($text);
?>
A5 &gt; 3 &amp; 2 &lt; 4
B5 > 3 & 2 < 4
C5 &gt; 3 & 2 &lt; 4
D5 &gt; 3 &amp; 2 < 4
Attempts:
2 left
💡 Hint
Default flags convert &, <, and > but not quotes.
Predict Output
advanced
2:00remaining
What error occurs with invalid encoding in htmlspecialchars?
What happens when you run this PHP code?
PHP
<?php
$input = "\xB1\x31";
echo htmlspecialchars($input, ENT_QUOTES, 'UTF-8');
?>
AWarning: htmlspecialchars(): Invalid byte sequence in UTF-8
BOutputs: \xB1\x31
COutputs: &#177;1
DFatal error: htmlspecialchars() expects parameter 3 to be string
Attempts:
2 left
💡 Hint
Invalid byte sequences cause warnings in htmlspecialchars with UTF-8.
🧠 Conceptual
advanced
2:00remaining
Why use htmlspecialchars instead of htmlentities?
Which reason best explains why htmlspecialchars is preferred over htmlentities for output escaping?
Ahtmlentities converts fewer characters, so it is less secure
Bhtmlspecialchars only converts essential characters, making output more readable and faster
Chtmlspecialchars converts all characters to entities, preventing all XSS attacks
Dhtmlentities does not convert quotes by default
Attempts:
2 left
💡 Hint
Think about performance and readability.
Predict Output
expert
2:00remaining
How many characters are in the output of htmlspecialchars with ENT_NOQUOTES?
Given this PHP code, how many characters will the output string have?
PHP
<?php
$input = "<a href='test'>Click & Learn</a>";
$output = htmlspecialchars($input, ENT_NOQUOTES);
echo strlen($output);
?>
A31
B35
C33
D29
Attempts:
2 left
💡 Hint
Count how many characters are replaced and how many new characters each replacement adds.