>You forgot `ENT_QUOTES | ENT_SUBSTITUTE` on `htmlspecialchars`, it's perilous to use directly in every single place.
I would have added it in real code... but manually escaping everything is still what you have to do with raw php. At the very least, have something to recursively escape an array (keys and values) before it gets dumped onto the page.
Twig at least lets you escape by default (and you don't have to worry about double-escaping), but the equivalent wouldn't be much less verbose. Twig's syntax is a bit cleaner in couple of places I think, and it lets you use array bracket syntax [] in versions of PHP too old to support it natively.
{% for row in results %}
<div>{{ row.field }}</div>
{% endfor %}
Of course this doesn't include the added complexity of an entire framework implementing its own language running on top of everything but still, the environment is a bit more sane.