Search code examples
phpfor-loopif-statementmultiplication

If statement on a number and it's multipliers


I want to make a loop that works in 2 ways , if period is anynumber but 13 and it's multiplications , it will call the last income I had , if not it should make the equations you see below , any idea how to ?

for ($i=1; $i <= $period ; $i++) { 
         if ($i+=13) {
         echo round($income=$income*(1+($rowage/100))). "<br>";
         } else
         {
        echo $income. "<br>";
         }
    }

Solution

  • It is called mutiple of 13, and you can use modulos:

    for ($i=1; $i <= $period ; $i++) { 
         if ($i%13==0) {
         echo round($income=$income*(1+($rowage/100))). "<br>";
         } else
         {
        echo $income. "<br>";
         }
    }
    

    $i%13==0 means if division of i by 13 gives 0, which should be true if i is multiple of 13.