Click here to Skip to main content
15,567,221 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Can someone help, don’t know how to solve this using loop in php. 


What I have tried:

$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
Posted
Updated 29-Oct-21 2:05am

1 solution

Try this:
PHP
foreach ($pay as $val) {
    $Capital -= $val;
    echo "Capital value: $Capital\n";
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900