Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to decrement number by 2 for loop in php [closed]

Tags:

php

for-loop

The following is a simplified version of my code:

for($n10 = 10; $n10 = 1; $n10--2{
    echo $n10;
};

I want the number in the second paragraph to increment by 2 with each loop, e.g.

10, 8, 6, 4

Any help would be much appreciated. Thanks!

like image 746
user3901528 Avatar asked Dec 15 '22 12:12

user3901528


1 Answers

The last component can be seen like a block of code, so you can do the following...

for($index=10;$index > 0;$index-=2) {
    echo $index;
}

The -= is equivalent to $index = $index - 2

like image 157
Guy Park Avatar answered Dec 25 '22 11:12

Guy Park