-1

Can some help, don’t know how to solve this using loop in PHP

$pay = [20, 40 , 89, 300, 190, 15]; <br/>
$Capital = 1000; <br/>

I want the loop to achieve this result

1000-20 = 980 <br/>
980-40 = 940 <br/>
940-89 = 851 <br/>
851-300 = 551 <br/>
551-190 = 361 <br/>
361-15 = 346 <br/>

My code is:

$newbal = $Capital-$pay <br/>
for ($amount=$newbal; $amount>=$Capital; $amount-=$pay) {
    echo “{$amount} ”; <br/>
    $amount++; <br/>
}

My code is giving me this result:

1000-20 = 980 <br/>
1000-40 = 960 <br/>
1000-89 = 911 <br/>
1000-300 = 700 <br/>
1000-190 = 810 <br/>
1000-15 = 985 <br/>
Gert Arnold
  • 105,341
  • 31
  • 202
  • 291
Segun
  • 29
  • 5
  • 5
    Beware of coding in a Word Processor! The non ascii quotes like `“` will almost always cause you issues – RiggsFolly Oct 29 '21 at 14:33
  • 2
    The code you've shown produces an error. It does not produce the stated result: http://sandbox.onlinephpfunctions.com/code/f17d08c1ba4cd3bc28735fadeb470f643f87baa9 – ADyson Oct 29 '21 at 14:35

5 Answers5

3

A simple foreach loop over the $pay array with a $balance variable will allow a simple calculation inside the loop:

$pay = [20, 40 , 89, 300, 190, 15];
$Capital = 1000; 

$balance = $Capital;
foreach ($pay as $p) {
    printf("%d - %d = %d\n", $balance, $p, $balance = $balance - $p);
}                                          ########################

Output:

1000 - 20 = 980
980 - 40 = 940
940 - 89 = 851
851 - 300 = 551
551 - 190 = 361
361 - 15 = 346
hakre
  • 193,403
  • 52
  • 435
  • 836
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
2

Instead of using a complicated for loop, I'd recommend using a foreach to loop over the $pay array and override $Capital after the echo.

We can use the "${}" syntax to use variables inside strings.


Consider this example:

<?php

$pay = [20, 40 , 89, 300, 190, 15];
$Capital = 1000; 
$newBal = $Capital;

foreach ($pay as $amount) {
    
    echo "${Capital} - ${amount} = " . ($Capital - $amount) . PHP_EOL;
    $Capital -= $amount; 
}

This will output the following as you can test in this online demo

1000 - 20 = 980
980 - 40 = 940
940 - 89 = 851
851 - 300 = 551
551 - 190 = 361
361 - 15 = 346

We can do the assignment of $Capital inline, so I'd read:

echo "${Capital} - ${amount} = " . ($Capital = $Capital - $amount) . PHP_EOL;

But for sake of readability, I'd add an extra line as in my example.

0stone0
  • 34,288
  • 4
  • 39
  • 64
2

You've somewhat overcomplicated it, and the code you showed produces errors rather than the output you stated.

You can achieve this more simply by looping through the $pay array directly with a foreach, and reducing the balance by the amount in the current pay item each time:

$pay = [20, 40 , 89, 300, 190, 15];
$capital = 1000;
$balance = $capital;
    
foreach ($pay as $item)
{
  $output = $balance."-".$item." = ";
  $balance -= $item;
  $output .= $balance;
  echo $output.PHP_EOL;
}

Working demo: http://sandbox.onlinephpfunctions.com/code/dd1189f2d48b3428986ce52a087fb425ee970621

ADyson
  • 57,178
  • 14
  • 51
  • 63
1
<?php

$pay = [20, 40 , 89, 300, 190, 15];
$Capital = 1000;

foreach ($pay as $amount) {
    $newCap = $Capital - $amount; // calculate the new amount for output
    echo $Capital . ' - ' . $amount . ' = ' . $newCap . '<br>'; // output it
    $Capital = $newCap; // update the new amount for the next iteration
}

Online demo: https://3v4l.org/qmqcv

WOUNDEDStevenJones
  • 5,150
  • 6
  • 41
  • 53
0

The code below, loops through the $pays array of integers.

each loop calculates the equation of $captial minus one of the integers and repeats, this is calculated and stored as a string in the $lines variable and at the end of each loop we recalculate the $capital and update the new value of $capital based on the previous calculation of capital = capital - pay

<?php
    $pays = [20, 40 , 89, 300, 190, 15];
    $capital = 1000;

    $lines = [];

    foreach($pays as $pay){
        $lines[] = $capital . "-" . $pay . " = " . ($capital - $pay) . "<br/>";
        $capital -= $pay;
    }

    echo implode("\n", $lines);

?>
Dean Van Greunen
  • 5,060
  • 2
  • 14
  • 28