How can PHP be used to detect mobile devices and load specific CSS stylesheets accordingly?

To detect mobile devices and load specific CSS stylesheets accordingly in PHP, you can use the $_SERVER['HTTP_USER_AGENT'] variable to check the user agent string of the device accessing the website. Based on the user agent string, you can then conditionally load different CSS stylesheets using PHP.

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

if (strpos($user_agent, 'Mobile') !== false || strpos($user_agent, 'Android') !== false) {
    echo '<link rel="stylesheet" type="text/css" href="mobile.css">';
} else {
    echo '<link rel="stylesheet" type="text/css" href="desktop.css">';
}
?>