How can the user improve the maintainability of their PHP code for mobile device detection?
Issue: The user can improve the maintainability of their PHP code for mobile device detection by encapsulating the detection logic into a separate function. This will make the code more modular and easier to manage in the future. Code snippet:
function isMobileDevice() {
$userAgent = $_SERVER['HTTP_USER_AGENT'];
$mobileKeywords = array('Mobile', 'Android', 'Silk', 'Kindle', 'BlackBerry', 'Opera Mini', 'Opera Mobi');
foreach ($mobileKeywords as $keyword) {
if (stripos($userAgent, $keyword) !== false) {
return true;
}
}
return false;
}
// Example of how to use the function
if (isMobileDevice()) {
// Code for mobile devices
} else {
// Code for non-mobile devices
}