Fabian Amran Fabian Amran - 27 days ago 7
PHP Question

How to add 300 to a variable on each iteration using a for loop in php?

I am trying to make a loop that iterates 6 times, on each iteration I want to add 300 to the variable $count.

$count = 300;
for ($i=0; $i < 6 ; $i++) {
$count + 300;
echo $count; //300300300300300300
}


The results I wanted to achieve are

echo $count // 300600900120018002400


How can I acheive these results?

Answer

Try this, assign value to $count. $count = $count + 300;

<?php
 $sum_number = 300;
 $count = 0;
 for ($i=0; $i < 6 ; $i++) { 
     $count = $count + $sum_number;
     echo $count."\n"; 
 }
?>

DEMO

Comments