# Loop in PHP

**URL:** <https://www.phphelp.com/t/loop-in-php/34186>\
**Category:** Uncategorized\
**Created:** [October 28, 2021, 11:07pm UTC](https://www.phphelp.com/t/loop-in-php/34186 "2021-10-28T23:07:08Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ehrs](https://www.phphelp.com/letter_avatar_proxy/v4/letter/e/ecd19e/32.png) [@ehrs](https://www.phphelp.com/u/ehrs)\
**Post date:** [October 28, 2021, 11:07pm UTC](https://www.phphelp.com/t/loop-in-php/34186/1 "2021-10-28T23:07:08Z")

</div>

Can some help, i don’t know how to solve this using loop in php.  
$pay = [20, 40 , 89, 300, 190, 15];  
$Capital = 1000;  
I want the loop to achieve this result  
1000-20 = 980  
980-40 = 940  
940-89 = 851  
851-300 = 551  
551-190 = 361  
361-15 = 346

My code is:  
$newbal = $Capital-$pay  
for ($amount=$newbal; $amount\>=$Capital; $amount-=$pay) {  
echo “{$amount}  
”;  
$amount++;  
}  
My code is giving me this result:  
1000-20 = 980  
1000-40 = 960  
1000-89 = 911  
1000-300 = 700  
1000-190 = 810  
1000-15 = 985

---

<div class="post-metadata">

**Author:** ![whispers](https://www.phphelp.com/letter_avatar_proxy/v4/letter/w/858c86/32.png) [@whispers](https://www.phphelp.com/u/whispers)\
**Post date:** [October 29, 2021, 5:26pm UTC](https://www.phphelp.com/t/loop-in-php/34186/2 "2021-10-29T17:26:15Z")

</div>

Its because you never update the ‘capital’ amount to the new value.

Maybe something like this:

```auto
$pay = [20, 40 , 89, 300, 190, 15];
$capital = 1000;

for($i=0; $i<count($pay); $i++){
	echo $capital . ' - ' . $pay[$i] . ' = ' . ($capital-$pay[$i]) . '<br>';
	//update capital value
	$capital = ($capital-$pay[$i]);
}

```
