How can PHP be used to determine the operating system of a user visiting a website?

To determine the operating system of a user visiting a website using PHP, you can utilize the $_SERVER['HTTP_USER_AGENT'] variable which contains information about the user's browser and operating system. By parsing this variable, you can extract information about the user's operating system.

$user_agent = $_SERVER['HTTP_USER_AGENT'];

if (strpos($user_agent, 'Windows') !== false) {
    echo "User is using Windows operating system";
} elseif (strpos($user_agent, 'Macintosh') !== false) {
    echo "User is using Macintosh operating system";
} elseif (strpos($user_agent, 'Linux') !== false) {
    echo "User is using Linux operating system";
} else {
    echo "Unable to determine user's operating system";
}