What are some methods to differentiate between mobile and desktop users on a website using PHP?

One method to differentiate between mobile and desktop users on a website using PHP is by checking the user agent string in the HTTP request. Mobile devices often have specific strings in their user agent that can be used to identify them.

// Check if the user agent indicates a mobile device
function isMobile() {
    return preg_match('/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce|xda|xiino/i', $_SERVER['HTTP_USER_AGENT']);
}

if (isMobile()) {
    echo "You are using a mobile device.";
} else {
    echo "You are using a desktop device.";
}