What are the best practices for outputting HTML code as plain text in PHP without rendering it as a live element?

When outputting HTML code as plain text in PHP, it's important to prevent the browser from interpreting it as live HTML elements. To achieve this, you can use the htmlspecialchars() function to encode the HTML entities and display them as plain text on the webpage.

<?php
$htmlCode = "<h1>Hello, World!</h1>";
echo htmlspecialchars($htmlCode);
?>