加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP中出现意外的T_ELSE错误

发布时间:2020-12-13 22:21:31 所属栏目:PHP教程 来源:网络整理
导读:我正在研究一本来自php书籍的例子,并且在第8行使用此代码收到错误 ?php$agent = getenv("HTTP_USER_AGENT");if (preg_match("/MSIE/i","$agent"));{ $result = "You are using Microsoft Internet Explorer";}else if (preg_match("/Mozilla/i","$agent"));
我正在研究一本来自php书籍的例子,并且在第8行使用此代码收到错误

<?php

$agent = getenv("HTTP_USER_AGENT");
if (preg_match("/MSIE/i","$agent"));
{
    $result = "You are using Microsoft Internet Explorer";
}
else if (preg_match("/Mozilla/i","$agent")); 
{
    $result = "You are using Mozilla firefox";
}
else {$result = "you are using $agent"; }

echo $result;


?>

解决方法

尝试:

$agent = getenv("HTTP_USER_AGENT");
if (preg_match("/MSIE/i",$agent)) {
  $result = "You are using Microsoft Internet Explorer";
} else if (preg_match("/Mozilla/i",$agent)) {
  $result = "You are using Mozilla firefox";
} else {
  $result = "you are using $agent";
}

echo $result;

两件事情:

>你的if条款末尾有一个分号.这意味着后续的开括号是一个始终执行的本地块.这导致了一个问题,因为后来你有一个没有附加到if语句的else语句;和>不需要做“$agent”,不推荐.只需传入$agent.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读