非常基本的PHP if语句违反所有逻辑

时间:2017-01-13 10:30:52

标签: php if-statement math

发布这样一个简单的问题我感到很尴尬,但它是在13日星期五,而且我不知道它是否会对我的代码产生某种超自然的影响!

这是我的if语句;

Template template = myServiceReferenceClient.GetTemplate();

以下代码;

if ($invoice_grand_total > floatval($payment_details['TotalPayments'])) {

返回;

echo $invoice_grand_total . ' = ' . floatval($payment_details['TotalPayments'])

var_dump;

6.88 = 6.88

返回;

echo $outstanding_invoices_count . ' invoice_grand_total = ' . var_dump($invoice_grand_total) . '<br/>';
echo $outstanding_invoices_count . ' TotalPayments = ' . var_dump(floatval($payment_details['TotalPayments'])) . '<br/>';

不使用var_dump返回;

float(18) 0 invoice_grand_total =
float(18) 0 TotalPayments =
float(199) 0 invoice_grand_total =
float(199) 0 TotalPayments =
float(125) 0 invoice_grand_total =
float(125) 0 TotalPayments =
float(60) 0 invoice_grand_total =
float(60) 0 TotalPayments =
float(55) 0 invoice_grand_total =
float(55) 0 TotalPayments =
float(60) 0 invoice_grand_total =
float(60) 0 TotalPayments =
float(10) 0 invoice_grand_total =
float(10) 0 TotalPayments =
float(3.64) 0 invoice_grand_total =
float(3.64) 0 TotalPayments =
float(295) 0 invoice_grand_total =
float(295) 0 TotalPayments =
float(25.45) 0 invoice_grand_total =
float(25.45) 0 TotalPayments =
float(110) 0 invoice_grand_total =
float(110) 0 TotalPayments =
float(2.41) 0 invoice_grand_total =
float(2.41) 0 TotalPayments =
float(0.04) 0 invoice_grand_total =
float(0.04) 0 TotalPayments =
float(25.45) 0 invoice_grand_total =
float(25.45) 0 TotalPayments =
float(6.01) 0 invoice_grand_total =
float(6.01) 0 TotalPayments =
float(29.09) 0 invoice_grand_total =
float(29.09) 0 TotalPayments =
float(6.88) 0 invoice_grand_total =
float(6.88) 0 TotalPayments = 

如果它有帮助,这是完整的代码块;

0 invoice_grand_total = 18
0 TotalPayments = 18
0 invoice_grand_total = 199
0 TotalPayments = 199
0 invoice_grand_total = 125
0 TotalPayments = 125
0 invoice_grand_total = 60
0 TotalPayments = 60
0 invoice_grand_total = 55
0 TotalPayments = 55
0 invoice_grand_total = 60
0 TotalPayments = 60
0 invoice_grand_total = 10
0 TotalPayments = 10
0 invoice_grand_total = 3.64
0 TotalPayments = 3.64
0 invoice_grand_total = 295
0 TotalPayments = 295
0 invoice_grand_total = 25.45
0 TotalPayments = 25.45
0 invoice_grand_total = 110
0 TotalPayments = 110
0 invoice_grand_total = 2.41
0 TotalPayments = 2.41
0 invoice_grand_total = 0.04
0 TotalPayments = 0.04
0 invoice_grand_total = 25.45
0 TotalPayments = 25.45
0 invoice_grand_total = 6.01
0 TotalPayments = 6.01
0 invoice_grand_total = 29.09
0 TotalPayments = 29.09
0 invoice_grand_total = 6.88
0 TotalPayments = 6.88

为什么// outstanding invoice details echo '<fieldset> <legend>Outstanding Invoice Details</legend>'; echo '<table class="solid" style="margin:5px;">'; echo '<tr> <th>Invoice #</th> <th>Date</th> <th>Invoice Total</th> <th>Amount Paid</th> <th>Amount Due</th> </tr>'; $invoice_grand_total = 0.00; $outstanding_invoices_count = 0; $invoice_details_query = mysqli_query($con, "SELECT * FROM accounts_invoice WHERE invoice_customer_type='" . $invoice_customer_type . "' AND invoice_customer='" . $invoice_customer . "' AND invoice_posted='1' AND invoice_tenancy_id='" . $tenancy_id . "'") or die(mysql_error()); while($invoice_details_row = mysqli_fetch_array( $invoice_details_query )) { $invoice_grand_total = $invoice_details_row['invoice_total_amount_exc_vat'] + $invoice_details_row['invoice_total_vat_amount']; $payment_details_query = mysqli_query($con, "SELECT SUM(invoice_payment_amount) AS TotalPayments FROM accounts_invoice_payment WHERE invoice_payment_invoice_id='" . $invoice_details_row['invoice_id'] . "'") or die(mysql_error()); $payment_details = mysqli_fetch_array( $payment_details_query ); echo $outstanding_invoices_count . ' invoice_grand_total = ' . var_dump($invoice_grand_total) . '<br/>'; echo $outstanding_invoices_count . ' TotalPayments = ' . var_dump(floatval($payment_details['TotalPayments'])) . '<br/>'; if (floatval($invoice_grand_total) > floatval($payment_details['TotalPayments'])) { $outstanding_invoices_count++; echo '<tr> <td>' . $invoice_grand_total . ' = ' . floatval($payment_details['TotalPayments']) . '</td> <td><a href="view_invoice.php?invoice_id=' . $invoice_details_row['invoice_id'] . '" target="_blank">' . $invoice_details_row['invoice_id'] . '</a></td> <td>' . ConvertDate($invoice_details_row['invoice_date']) . '</td> <td>&pound;' . number_format($invoice_grand_total, 2, '.', ',') . '</td> <td>&pound;' . number_format($payment_details['TotalPayments'], 2, '.', ',') . '</td> <td>&pound;' . number_format(($invoice_grand_total - $payment_details['TotalPayments']), 2, '.', ',') . '</td> </tr>'; } } if ($outstanding_invoices_count==0) { echo '<tr><td colspan="10">There are no outstanding invoices.</td></tr>'; } echo '</table>'; if ($outstanding_invoices_count!=0) { echo '<table class="nobord">'; echo '<tr><td><span class="red">The above invoices are outstanding for this landlord. Click <a href="add_invoice_payment.php?invoice_customer_type=1&invoice_customer=' . $invoice_customer . '&invoice_tenancy_id=' . $tenancy_id . '">here</a> to add an invoice payment.</span></td></tr>'; echo '</table>'; } echo '</fieldset>'; 的if块在6.88 > 6.88条件下运行?

溶液;

我将if语句更改为;

6.88 = 6.88

1 个答案:

答案 0 :(得分:3)

通常你永远不会像这样比较浮点值的相等性,你需要使用最小的可接受差异:

Do not compare floating point numbers for equality.