What are the best practices for escaping characters, such as quotes, when outputting content in PHP?

When outputting content in PHP that may contain special characters like quotes, it's important to escape those characters to prevent potential security vulnerabilities or syntax errors. One common way to escape characters in PHP is by using the htmlentities() function, which converts special characters to HTML entities. This ensures that the content is displayed correctly and safely in the browser.

<?php
$content = "This is a string with 'quotes' and <html> tags";
echo htmlentities($content);
?>