How can PHP be used to determine the operating system and browser used by a user while surfing?

To determine the operating system and browser used by a user while surfing, you can use the $_SERVER superglobal array in PHP. The $_SERVER['HTTP_USER_AGENT'] variable contains information about the user's browser, operating system, and other details. By parsing this information, you can extract the operating system and browser details.

$user_agent = $_SERVER['HTTP_USER_AGENT'];

$browser = get_browser(null, true);
$browser_name = $browser['browser'];
$browser_version = $browser['version'];

$os = php_uname('s');

echo "Operating System: $os <br>";
echo "Browser: $browser_name $browser_version";