我有一個非常愚蠢的問題。我正在從 mysql 中提取收入和支出的總額,然后我想從收入中減去支出。由于某種原因,結果不是剩下的,而是顯示了收入數字 - 支出數字。代碼如下function total_by_type_between_dates($type, $start, $end){ global $uc_con; $start = strtotime ($start); $end = strtotime ($end); $sql = "SELECT SUM(amount) AS total FROM account WHERE `time` > $start && `time` < $end && `type` = '$type'"; $result = $uc_con->query($sql); $row = $result->fetch_array(MYSQLI_ASSOC); $total = number_format((float)$row['total'], 2, '.', ''); echo $total;}function total_expense_between_dates($start, $end){ global $uc_con; $start = strtotime ($start); $end = strtotime ($end); $sql = "SELECT SUM(amount) AS total FROM account WHERE `time` > $start && `time` < $end && `type` != 'income' && `type` != 'mileage'"; $result = $uc_con->query($sql); $row = $result->fetch_array(MYSQLI_ASSOC); $total = number_format((float)$row['total'], 2, '.', ''); return $total;}echo total_by_type_between_dates('income', $date_begining, $date_ending)-total_expense_between_dates($date_begining, $date_ending);奇怪的是結果顯示如下:2770.69-407.42而不是2363.29誰能告訴我可能是什么原因造成的
1 回答

狐的傳說
TA貢獻1804條經驗 獲得超3個贊
您的total_by_type_between_dates
函數正在回顯其結果而不是返回它。所以你的
echo total_by_type_between_dates('income', $date_begining, $date_ending)-total_expense_between_dates($date_begining, $date_ending);
有效地
echo total_by_type_between_dates('income', $date_begining, $date_ending); echo null-total_expense_between_dates($date_begining, $date_ending);
并且null
在數字上下文中被視為 0,因此您看到的結果是:2770.69-407.42
改變
echo $total;
在total_by_type_between_dates
到
return $total;
并且代碼將按預期工作。
- 1 回答
- 0 關注
- 186 瀏覽
添加回答
舉報
0/150
提交
取消