与';php mobile detect';和if语句

与';php mobile detect';和if语句,php,mobile,if-statement,Php,Mobile,If Statement,我正在使用一个名为的小脚本来识别正在使用的设备,以便使下载页面更友好 我使用的代码有点问题。当在计算机上运行此操作时,会输出所需的值,但是,当在移动设备(我拥有的设备是iPhone)上访问代码时,我会收到一个PHP错误,表示$deviceType未定义。我有一种强烈的感觉,我在if语句中做了一些不正确的事情 <?php require_once 'Mobile_Detect.php'; $detect = new Mobile_Detect; //Detect Platform if($d

我正在使用一个名为的小脚本来识别正在使用的设备,以便使下载页面更友好

我使用的代码有点问题。当在计算机上运行此操作时,会输出所需的值,但是,当在移动设备(我拥有的设备是iPhone)上访问代码时,我会收到一个PHP错误,表示
$deviceType
未定义。我有一种强烈的感觉,我在
if
语句中做了一些不正确的事情

<?php
require_once 'Mobile_Detect.php';
$detect = new Mobile_Detect;
//Detect Platform
if($detect->isMobile()):

    if($detect->isPhone()):
        if($detect->isiPhone()):
            $deviceType = 'iPhone';
        elseif($detect->isAndroid()):
            $deviceType = 'Android Phone';
        elseif($detect->isWindowsPhoneOS()):
            $deviceType = 'Windows Phone';
        elseif($detect->isGenericPhone()):
            $deviceType = 'Phone';

    endif;//End if Phone

    elseif($detect->isTablet()):
        if($detect->isiPad()):
            $deviceType = 'iPad';
        elseif($detect->isAndroid()):
            $deviceType = 'Android Tablet';
        elseif($detect->isKindle()):
            $deviceType = 'Kindle';
        elseif($detect->isGenericTablet()):
            $deviceType = 'Tablet';
    endif;//End ifTablet

endif;//End ifMobile

//If not mobile it must be a computer
else:
    $deviceType = 'computer';
endif;// End Detect Platform

?>

<p>This is a <b><?php echo $deviceType; ?>

这是一个
任何帮助都将不胜感激

问候,


当你使用iPhone时,Ian(显然)没有设置$deviceType。消除错误的一种方法是在顶部声明
$deviceType
。但这并不能完全解决你的问题(你的iPhone仍然没有被检测到)

查看移动检测的文档,我在任何地方都看不到
isPhone()。这是真函数吗

--

(可能)与您的问题无关,但您可能还希望在每组if/else之后添加一个通用的
else
语句,以捕获任何因任何原因不满足
isGenericPhone()
isGenericTablet()
的情况。事实上,我会去掉这些,只使用
else


顺便说一句,可选的if/else语法很烦人,使代码很难阅读。

在不混合使用php/html时,不要使用可选语法。很难看。您也没有默认的“未知移动设备”,以防所有if()测试都失败。您好,谢谢您的评论。即使我对
{}
使用语法,结果仍然不会改变。关于未知的移动设备-我认为使用
isGenericPhone()
可以解决这个问题。然而,我正在iPhone上测试它,因此它应该被识别(如果代码工作正常的话)。再次感谢你的帮助。是的!你说得对-
isPhone
不存在-我不知道我从哪里得到这个主意的!谢谢