【发布时间】:2015-12-17 23:06:04
【问题描述】:
我有一些 if/else 语句的验证。
<?php
if (isset($_POST["join"])) {
if ($userpoint < $lessonpoint) { //pt
echo "you need more points";
} //pt
else { //has enough point
if ($row['num'] > 0) { //check if user took this lesson
echo "you took this lesson before.";
} //check if user took this lesson ends
else { //then let him apply to database
//define post:
$postvalue = (int)$_POST["postvalue"];
//and check
if($postvalue == '' || $postvalue <= 0 || $postvalue > $minimumpostvalue || $postvalue == is_int($postvalue)) { //check post
echo "Error.";
} //checkpost ends.
else { //insert
$sql = "INSERT into etc... VALUES (?, ?, ?)";
if($sql){ //to another database
$artibir = "UPDATE etc.";
echo "Done.";
} // to another database
}//insert
} //let him apply
} //has enough point
} //if post isset join
?>
这很好用。
但我想针对这种情况发出另一条错误消息:$postvalue > $minimumpostvalue
在尝试时,我迷失在 if/else 语句中。 无论我在哪里放置新语句,我都会遇到错误。
所有变量都已定义。
我可以在哪里以及如何放置 $postvalue > $minimumpostvalue 来回显不同的错误消息?
【问题讨论】:
-
您的代码可能运行良好,但编码不正确 - 就像一道菜味道很好,但它的食谱太糟糕了,原来的厨师无法理解,这就是正在发生的事情。尝试尝试如何更好地编写它。我建议从缩进开始。
-
这已经是并且将成为维护的噩梦。您迷失在 if-else-statments 中的事实大喊:代码异味。将您的代码重构为函数顶部 15 行,尽可能避免使用 else 语句,并尽早返回。
标签: php if-statement