How can PHP scripts be optimized to ensure compatibility with different web browsers, such as Firefox and Internet Explorer?

One way to optimize PHP scripts for compatibility with different web browsers is to use conditional statements to detect the user's browser and serve specific code accordingly. This can help ensure that the script functions correctly across various browsers like Firefox and Internet Explorer.

<?php
$browser = $_SERVER['HTTP_USER_AGENT'];

if (strpos($browser, 'Firefox') !== false) {
    // Code specific for Firefox
} elseif (strpos($browser, 'MSIE') !== false || strpos($browser, 'Trident') !== false) {
    // Code specific for Internet Explorer
} else {
    // Default code for other browsers
}
?>