How can cross-browser compatibility issues be addressed when using JavaScript in PHP applications?

Cross-browser compatibility issues can be addressed by using feature detection rather than browser detection. This involves checking if a particular feature is supported by the browser before using it in the JavaScript code. This approach ensures that the code will work across different browsers without relying on specific browser versions.

<?php
echo '<script>';
echo 'if (document.addEventListener) {';
echo '    // code that uses addEventListener';
echo '} else if (document.attachEvent) {';
echo '    // code that uses attachEvent';
echo '}';
echo '</script>';
?>