2017-01-13 123 views
0

我覺得很尷尬發佈這樣一個簡單的問題,但它是星期五13日,我不知道這是否對我的代碼有某種超自然效果!非常基本的PHP語句如果違反所有邏輯

這是我的if語句;

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

以下代碼;

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

return;

6.88 = 6.88 

var_dump;

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/>'; 

返回;

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 = 

沒有使用var_dump返回;

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>'; 

爲什麼6.88 > 6.88運行的,如果塊儘管條件6.88 = 6.88

SOLUTION;

我改變了我的if語句;

if (round($invoice_grand_total, 2) > round($payment_details['TotalPayments'], 2)) { 
+0

請你添加一些var_dumps?什麼數據類型是$ invoice_grand_total和$ payment_details ['TotalPayments']? –

+0

你用過var_dump嗎?曾經考慮過浮點精度?比較'bin2hex(pack(「d」))'如果你不確定。 – SOFe

+1

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

回答

3

通常你從來沒有像這樣相等比較浮點值,你需要使用一個最小可接受的差異:

Do not compare floating point numbers for equality.

+0

非常感謝您的支持,但是隻有在工作到小數點後兩位時才重要?此外,找出爲什麼我當前的代碼不工作,因爲它似乎違反邏輯將是很有趣的 –

+0

浮點數不是100%準確的!您在PHP中的計算可能返回10.00000000001,這不等於10. – NID

+0

在比較它們之前,使用sprintf(http://php.net/manual/en/function.sprintf.php)格式化浮點數。 – NID