How to detect mobile device using PHP?

by using this script you can detect a mobile device

<?php

/* detect mobile device*/
$ismobile = 0;
$container = $_SERVER&#91;'HTTP_USER_AGENT'&#93;;

// A list of mobile devices
$useragents = array (
                'Blazer' ,
                'iPhone' ,
                'iPad' ,
                'Palm' ,
                'Handspring' ,
                'Nokia' ,
                'Kyocera',
                'Samsung' ,
                'Motorola' ,
                'Smartphone',
                'Windows CE' ,
                'Blackberry' ,
                'WAP' ,
                'SonyEricsson',
                'PlayStation Portable',
                'LG',
                'MMP',
                'OPWV',
                'Symbian',
                'EPOC',
                );

foreach ( $useragents as $useragents ) {
    if(strstr($container,$useragents)) {
        $ismobile = 1;
    }
}
if ( $ismobile == 1 ) {
    //code for mobile devices
}else {
    //code for web
}
?>