What are some best practices for handling PHP code in Joomla templates to avoid breaking the frontend?

When including PHP code in Joomla templates, it's important to follow best practices to avoid breaking the frontend. One common issue is using PHP code directly in template files, which can lead to syntax errors or unexpected behavior. To prevent this, it's recommended to use Joomla's built-in methods for executing PHP code within templates, such as using the JHTML class.

<?php
// Example of using JHTML::script to include a JavaScript file in a Joomla template
JHTML::script('path/to/script.js');
?>