What are the best practices for including external JavaScript files in PHP scripts to avoid unintended behaviors or conflicts?

When including external JavaScript files in PHP scripts, it is important to ensure that there are no conflicts or unintended behaviors. One way to achieve this is by properly encapsulating the JavaScript code within a function or object to avoid global scope pollution. Additionally, using a unique namespace for the JavaScript code can help prevent conflicts with other scripts.

<?php
echo '<script>';
echo '(function() {';
echo '   // Your JavaScript code here';
echo '})();';
echo '</script>';
?>