"Addition" doesnt work (for me)

I have a line of code that says $revenue = $revenue + $amount. The code loops through and reads data from a CSV file and assigns one of the fields to $amount. The program is supposed to accumulate $revenue. $amount is populated but $revenue is always zero (I am very new to PHP)

Not sure if I should upload the whole code, so here is the line in question for now

$revenue = $revenue + $amount;

We need to see the code.

here is the code

<html>
<head>
<link rel="stylesheet" type="text/css" href="style2.css">
<title>This is my Title</title>
<body>
<?php


$revenue = 0.00;
$costs = 0.00;  
$lastorderid = "" ;
$lastsku = "" ;
$lastproduct = "" ;

$file = fopen("Report.txt", "r");


echo '<table id="products">
  <tr>
    <th>Order ID</th>
    <th>SKU</th>
    <th>Product</th>
    <th>Revenue</th>
    <th>Costs</th>
  </tr> ';

while (($data = fgetcsv($file,0,"\t"))!== FALSE) {

$nextorderid = $data[1] ;
$nextsku = $data[2] ;
if ($nextorderid == $lastorderid) {
		// Accumulate values
		
		$paymenttype = $data[4] ;
		$paymentdetail = $data[5] ;
	
		$amount = $data[6] ;
		$quantity = $data[7] ;
		if ($paymenttype == "Product charges" && $paymentdetail == "" || 
		    $paymenttype == "Other" && $paymentdetail == "Product Tax" || 
		    $paymenttype == "Other" && $paymentdetail == "Shipping tax" || 
			$paymenttype == "Other" && $paymentdetail == "Shipping" || 
			$paymenttype == "Promo rebates" && $paymentdetail == "") {
			
					
			$revenue  = $revenue + $amount ;
						
			echo 'Revenue ' . $revenue . '  Amount ' . $data[6] . '<br>';
		
		
		} else {
			
			$costs  += $amount;	
			
			echo 'Costs ' . $costs . '  Amount ' . $amount . '<br>';
			
			
		
		}
		
} else {

echo '
  <tr>
    <th>' . $lastorderid . ' </th>
    <th>' . $lastsku . ' </th>
    <th>' . $lastproduct . ' </th>
    <th>' . $revenue . ' </th>
    <th>' . $costs . ' </th>
  </tr>' ;

$revenue = 0;
echo 'zero amount';
echo '<br>';
$costs = 0;  
$lastorderid = $data[1] ;
$lastsku = $data[2] ;

  
 
}

}
 
echo '	   </table>';
	   

?>

We also need to see (a sample of) your input data.

I suspect the amount contains a currency symbol which would cause it to be treated as zero as a number, which would look okay to you when you echo it but makes no sense to the computer trying to use it as a number. If you do have a currency symbol as part of the data, you either need to remove it from the csv data and only display the currency symbol when you display values (this is the preferred solution) or you need to strip it off before using the value in a calculation (not the preferred solution.) Also, if you have commas as 1000’s separators in the numbers, these should also be removed from the csv data and only used when you display values. Both currency symbols and commas as 1000’s separators are human conventions to make values more readable to us, but don’t work with a computer trying to use the values as numbers.

Hi you suspect correctly, kit was a £ symbol, I had someone on “fiverr” take a look and they made a change $amountvalue = str_replace(“£”, “”, $amount);

Sponsor our Newsletter | Privacy Policy | Terms of Service