我收到这个特殊的警告:
警告:在C:'xampp'htdocs'OnlineQuiz'Resultpage.php中除零第98行
我使用了除法表达式。像这样:
Ratio = Correct answers/Attempted questions
如果两者都为0,我将得到警告。但我不想这样。我只想要一条消息,而不是这个错误。如何处理这个问题?
通过在操作前添加@
符号来忽略错误:
Ratio = @Correct answers/Attempted questions
http://php.net/manual/en/language.operators.errorcontrol.php 在操作前添加@
符号以忽略错误报告/消息
Ratio = @Correct_answers / $Attempted_questions
除法前需要检查$Attempted_questions
不为零:
if ($Attempted_questions != 0) {
$Ratio = $Correct_answers / $Attempted_questions
}
您可以简单地使用try..catch
和throw
作为
function makeratio($Correct_answers, $Attempted_questions) {
if (!$Attempted_questions) {
throw new Exception('Division by zero');
}
return $result = ($Correct_answers / $Attempted_questions);
}
try {
echo makeratio($Correct_answers, $Attempted_questions);
} catch (Exception $e) {
echo 'Caught Exception : ' . $e->getMessage();
}