我当前设置了一个登录表单,这使我的用户感到困惑。 我处理错误的方式是这样的;
if (!($result->total > 0)) {
$err[] = "License key is not in our system.";
}
if ($claimed == 1) {
err[] = 'License key has been claimed already.';
}
if ($userID > 0) {
$err[] = 'License key is already connected to a user.';
}
if ($banned == 1) {
$err[] = 'License key is banned';
}
因此,例如,如果我的一个用户输入无效的许可证密钥,而不是显示它不在我们的系统中,它将显示已禁止(造成混淆(。因为我没有退出代码并让它运行。 我想知道如何在这样设置我的函数时继续进行错误处理。 更新- 忘了显示我如何显示错误。.我的错!
if (empty($err)) {
//no errors
} else {
echo $err; //this will show the last error instead of the first error generated
}
OK Bob,
如果您向我们展示您如何呈现错误,那将很有帮助,因为您正在解释的内容将表明您的$err
数组将包含两个值,而不仅仅是(最后一个(值。
然而,我认为这里发生的事情是,你的$banned
条件将永远得到满足;除非你在if
语句中添加另一个=
,就像这样:
if (!($result->total > 0)) {
$err[] = "License key is not in our system.";
}
if ($claimed == 1) {
err[] = 'License key has been claimed already.';
}
if ($userID > 0) {
$err[] = 'License key is already connected to a user.';
}
if ($banned == 1) { # <-- Here
$err[] = 'License key is banned';
}
然后,出于测试目的,您可以查看错误数组:
if(isset($err) && !empty($err)){
print_r($err);
}
如果要遍历每个潜在错误:
if(isset($err) && !empty($err)){
foreach($err as $error){
echo "Error because: {$error}".PHP_EOL;
}
}
因此,您将所有错误添加到数组err
中。
要显示数组中的第一项,只需使用[0]
访问第一个索引。
if (empty($err)) {
//no errors
} else {
echo $arr[0];
}