How to detect browsers in PHP?

In this blog, we will see how we can detect the browser name and its information by using PHP code.

In PHP, there is a global variable known as $_SERVER. This variable is mostly used for printing the server and the environment information.

After printing the $_SERVER[‘HTTP_USER_AGENT’], we will receive the information about the browser.

Depending on the browser, the output can be very. To overcome such kinds of issues, a specific code should be written.

you can use the getBrowser() function to detect the browser with PHP with the following code:

function getBrowserInfo() {

$user_agent = $_SERVER['HTTP_USER_AGENT'];
$browser = "N/A";

$browsers = array(
'/msie/i' => 'Internet explorer',
'/firefox/i' => 'Firefox',
'/safari/i' => 'Safari',
'/chrome/i' => 'Chrome',
'/edge/i' => 'Edge',
'/opera/i' => 'Opera',
'/mobile/i' => 'Mobile browser'

foreach ($browsers as $regex => $value) {
if (preg_match($regex, $user_agent)) { $browser = $value; }

return $browser;

echo "Browser Information : " . getBrowserInfo();

So, thanks for following this blog, and if you need any help please leave your queries in the comment box we will be happy to help you.