Jump to content

Recommended Posts

Hi all. I am having an issue with PHP comparing variables and returning inconsistent (and i think incorrect) results. Here is my script:

function isItMeOr($invoiceId) {

// Tally invoice amount
$invTotal = 0;
$sql8 = mysql_query("SELECT qty, discountPercent, amount
	FROM Invoice_Items
	WHERE Invoice_Items.invoiceId = '$invoiceId'
	AND Invoice_Items.active = '1'");
while ($result8 = mysql_fetch_array($sql8)) {
	$qty = $result8['qty'];
	echo "qty: $qty\n";
	$amount = $result8['amount'];
	echo "amount: $amount\n";
	$discountPercent = $result8['discountPercent'];
	echo "discountPercent: $discountPercent\n";
	$invTotal = $invTotal + ($qty * round(($amount - ($amount * ($discountPercent / 100))), 2));
	echo "invTotal: $invTotal\n";
}
echo "\n";

// Tally payments
$invPayments = 0;
$sql9 = mysql_query("SELECT amount FROM Payment_Allocations WHERE invoiceId = '$invoiceId'");
while ($result9 = mysql_fetch_array($sql9)) {
	$payAmount = $result9['amount'];
	echo "payAmount: $payAmount\n";
	$invPayments = $invPayments + $payAmount;
	echo "invPayments: $invPayments\n";
}
echo "\n";

// Compare
echo "invTotal: $invTotal (" . gettype($invTotal) . ")\n";
echo "invPayments: $invPayments (" . gettype($invPayments) . ")\n";
if ($invTotal == $invPayments) {
	echo "invTotal == invPayments\n";
}
if ($invTotal < $invPayments) {
	echo "invTotal < invPayments\n";
}
if ($invTotal > $invPayments) {
	echo "invTotal > invPayments\n";
}

// Convert to string and compare again
$invTotalAsStr = (string) $invTotal;
$invPaymentsAsStr = (string) $invPayments;
echo "invTotalAsStr: $invTotalAsStr (" . gettype($invTotalAsStr) . ")\n";
echo "invPaymentsAsStr: $invPaymentsAsStr (" . gettype($invPaymentsAsStr) . ")\n";
if ($invTotalAsStr == $invPaymentsAsStr) {
	echo "invTotalAsStr == invPaymentsAsStr\n";
}
if ($invTotalAsStr < $invPaymentsAsStr) {
	echo "invTotalAsStr < invPaymentsAsStr\n";
}
if ($invTotalAsStr > $invPaymentsAsStr) {
	echo "invTotalAsStr > invPaymentsAsStr\n";
}

echo "\n\n";
}

isItMeOr('15126');
isItMeOr('15171');

 

And here is the output:

 

qty: 1
amount: 55
discountPercent: 0
invTotal: 55
qty: 1
amount: 49.95
discountPercent: 0
invTotal: 104.95

payAmount: 104.95
invPayments: 104.95

invTotal: 104.95 (double)
invPayments: 104.95 (double)
invTotal == invPayments
invTotalAsStr: 104.95 (string)
invPaymentsAsStr: 104.95 (string)
invTotalAsStr == invPaymentsAsStr


qty: 1
amount: 64.9
discountPercent: 0
invTotal: 64.9
qty: 1
amount: 49.95
discountPercent: 0
invTotal: 114.85
qty: 1
amount: 99
discountPercent: 0
invTotal: 213.85

payAmount: 213.85
invPayments: 213.85

invTotal: 213.85 (double)
invPayments: 213.85 (double)
invTotal > invPayments
invTotalAsStr: 213.85 (string)
invPaymentsAsStr: 213.85 (string)
invTotalAsStr == invPaymentsAsStr

 

As you can see, the results for invoice '15126' come back as you would expect. The result for '15171' says invTotal > invPayments even though they are the same. Changing them to strings fixes the issue, however I'd still like to know why it's happening in the first place. Does anyone know what's going on here or what I can do to avoid such a pitfall in future? Thanks

Link to comment
https://forums.phpfreaks.com/topic/240577-php-comparison-issues-with-mysql-data/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.