...
The code does use some techniques which are worth understanding:
We use an array of string and concatenate them at the end for performance.
We escape characters like & < and > to translate into HTML entities so that data can be not mis-interpreted and HTML markup.
We use a magic token to handle the convention of allows a double $$ to represent a $ character.