本文介绍了PHP中出现意外的T_ELSE错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在处理一本php书中的示例,并在此代码的第8行出现错误
I am working on an example from a php book and am getting an error on line 8 with this code
<?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子句的末尾有分号.这意味着随后的开括号是始终执行的局部程序段.这引起了一个问题,因为后来您有一个
else
语句未附加到if
语句;和
You had a semi-colon at the end of your if clauses. That means the subsequent opening brace was a local block that is always executed. That caused a problem because later you had an
else
statement that wasn't attached to anif
statement; and
不必执行"$agent"
,因此不建议这样做.只需传递$agent
.
Doing "$agent"
is unnecessary and not recommended. Simply pass in $agent
.
这篇关于PHP中出现意外的T_ELSE错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!