How can PHP be used to detect if a user is accessing a website from a mobile device?

To detect if a user is accessing a website from a mobile device using PHP, you can check the user agent string sent by the browser. Mobile devices often have specific user agent strings that can be used to identify them. By parsing the user agent string in PHP, you can determine if the user is accessing the website from a mobile device.

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

if (strpos($user_agent, 'Mobile') !== false || strpos($user_agent, 'Android') !== false) {
    echo "User is accessing the website from a mobile device.";
} else {
    echo "User is not accessing the website from a mobile device.";
}
?>