0
0
PHPprogramming~10 mins

Output escaping with htmlspecialchars in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Output escaping with htmlspecialchars
Start with raw string
Apply htmlspecialchars()
Convert special chars to HTML entities
Safe output to browser
End
This flow shows how raw text is converted safely for HTML output by replacing special characters with HTML entities.
Execution Sample
PHP
<?php
$input = "<b>Hello & Welcome</b>";
$safe = htmlspecialchars($input);
echo $safe;
?>
This code converts special HTML characters in a string to safe entities and prints the safe string.
Execution Table
StepVariableValue BeforeActionValue AfterOutput
1$inputnullAssign raw string<b>Hello & Welcome</b>
2$safenullApply htmlspecialchars()&lt;b&gt;Hello &amp; Welcome&lt;/b&gt;
3echoPrint $safe&lt;b&gt;Hello &amp; Welcome&lt;/b&gt;
4--End of script--
💡 Script ends after printing escaped string, preventing HTML injection.
Variable Tracker
VariableStartAfter Step 1After Step 2Final
$inputnull<b>Hello & Welcome</b><b>Hello & Welcome</b><b>Hello & Welcome</b>
$safenullnull&lt;b&gt;Hello &amp; Welcome&lt;/b&gt;&lt;b&gt;Hello &amp; Welcome&lt;/b&gt;
Key Moments - 2 Insights
Why do we see &lt;b&gt; instead of <b> in the output?
Because htmlspecialchars() converts < and > to &lt; and &gt; to prevent the browser from interpreting them as HTML tags, as shown in step 2 and 3 of the execution_table.
What happens if we don't use htmlspecialchars() before echo?
The raw string with <b> tags would be interpreted as HTML, making text bold or causing security risks. The execution_table shows safe output only after htmlspecialchars() is applied.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of $safe after step 2?
A<b>Hello & Welcome</b>
BHello & Welcome
C&lt;b&gt;Hello &amp; Welcome&lt;/b&gt;
D&lt;b&gt;Hello & Welcome&lt;/b&gt;
💡 Hint
Check the 'Value After' column for $safe at step 2 in execution_table.
At which step is the raw string first assigned to $input?
AStep 3
BStep 1
CStep 2
DStep 4
💡 Hint
Look at the 'Action' column in execution_table to find when $input is assigned.
If htmlspecialchars() was not used, what would the output be at step 3?
A<b>Hello & Welcome</b>
BHello & Welcome
C&lt;b&gt;Hello &amp; Welcome&lt;/b&gt;
DError
💡 Hint
Consider what echo prints if $safe is not assigned and $input is printed raw.
Concept Snapshot
htmlspecialchars(string $input) converts special HTML chars like <, >, &, " to safe entities.
Use it before outputting user input to HTML to prevent code injection.
Example: htmlspecialchars("<b>Hi</b>") outputs &lt;b&gt;Hi&lt;/b&gt;.
Always escape output to keep web pages safe.
Full Transcript
This example shows how PHP's htmlspecialchars function changes special characters in a string to HTML entities. We start with a raw string containing HTML tags and an ampersand. Then, htmlspecialchars converts < to &lt;, > to &gt;, and & to &amp;. This prevents the browser from treating the string as HTML code. Finally, the safe string is printed. This protects the page from unwanted HTML or script injection. The execution table traces each step: assigning the raw string, applying htmlspecialchars, and printing the safe output. The variable tracker shows how $input stays the same while $safe changes to the escaped version. Key moments explain why escaping is needed and what happens without it. The quiz tests understanding of variable values and output at each step. Remember, always escape output with htmlspecialchars before showing user input in HTML.