请问家里的专业程序员,这段代码有什么问题?
每当我尝试运行它时,我都会收到此错误。
解析错误:语法错误,第 8 行 C:\xampp\htdocs\a\go.php 中意外的";">
该 php 代码:
<?php
$term=$_POST['term'];
$level=$_POST['level'];
if (
$term = 'First';
$level='js1';
)
{
header("Location: result.php");
exit();
}
elseif (
$term = 'First';
$level='js2';
)
{
header("Location: result2.php");
exit();
}
else {
$error = "Entry is invalid";
}
?>
检查你的 if 条件。if 和 else if 条件不得包含任何分号。如果您使用两个比较,则必须使用 &&或 ||在他们之间。如果您在 if 和 elseif 语句中使用 =,那么它将始终返回 true。
<?php
$term=$_POST['term'];
$level=$_POST['level'];
if ($term == 'First' && $level=='js1')
{
header("Location: result.php");
exit();
}
else if ($term == 'First' && $level='js2')
{
header("Location: result2.php");
exit();
}
else {
$error = "Entry is invalid";
}
?>
更改 if 条件
它应该是
if($term = 'First'&& $level='js1') or if($term = 'First'|| $level='js1')
elseif ($term = 'First' && $level='js2') or elseifif($term = 'First'|| $level='js2')
不
if ($term = 'First'; $level='js1';)
elseif ($term = 'First' ; $level='js2';)
所有if
语句的格式都不正确。您所做的只是在if
语句中设置变量。因此,您没有正确使用赋值运算符。if
语句的外观示例如下:
if($condition === true || $condition2 == 'hello'){
doSomething();
} else if($condition === false || $condtion2 == 'bye'){
doSomethingElse();
}
编辑:我还建议您提高代码缩进技能,这将真正有助于将来阅读您的代码。
在这些 if 中,您没有创建正确的布尔表达式。
if ($term === 'first' && $level === 'js1') {...}
elseif($term === 'First' && $level === 'js2') {...}
另外,我强烈建议你在重定向后放置一个die(;以避免不必要的负载(除非你需要在重定向后执行代码(。
试试这个。
<?php
$term=$_POST['term'];
$level=$_POST['level'];
if( $term == 'First' && $level=='js1'){
header("Location: result.php");
exit();
} elseif ( $term == 'First' && $level=='js2'){
header("Location: result2.php");
exit();
} else {
$error = "Entry is invalid";
}
?>
如果您收到一个标头已经发送错误,则将 ob_start(( 放在 php 文件的顶部,或者您也可以使用 javascript 进行此操作,如下所示。
<script>window.location.href = "a.php";</script>