Added phpDoc
Whitespace
Coding guidelines
Initialize variables in else block

Avoids PHP notices, fixes #16950
Convert user agent to lowercase

Improves performance by avoiding multiple case-insensitive string
operations.
Fix PHP Notice Undefined offset
Use static variable to improve performance