Latox
Latox

Reputation: 4695

php looping to add up a sum?

What do I use for a variable which will count the total amount?

For example

if ($featured == "1"){
    $cost = 100;
}else{
    $cost = 1000;
}

Basically each time that is looped, we display the $cost on page, but I also want to set aside a variable $total which adds up each $cost as the loop runs.

Upvotes: 0

Views: 94

Answers (1)

orlp
orlp

Reputation: 117691

I'm sorry, but really?

$total = 0; // this somewhere before the loop

// start loop
if ($featured == "1") {
    $cost = 100;
} else {
    $cost = 1000;
}

$total += $cost;
// end loop

Upvotes: 1

Related Questions