for ($i = 0; $i < 5; $i++) { echo $i.'<br/>'; } 

will output:

 0 1 2 3 4 

How to do what would be the opposite ie 4 3 2 1 ?

    2 answers 2

    Set countdown

     for ($i = 4; $i >= 0; $i--) { echo $i.'<br/>'; } 

    Only this is not sorting, but the reverse output of the array.

    If you do not need to include 0 in the output, you must remove the sign = from the condition $i >= 0


    Can be using while

     $i = 5; while ($i--) { echo $i . "<br />"; } Вывод: 4<br />3<br />2<br />1<br />0<br /> 
    • 2
      And it is possible and so for ($i = 0; $i < 5; $i++) { echo (4-$i).'<br/>'; } for ($i = 0; $i < 5; $i++) { echo (4-$i).'<br/>'; } - KoVadim
    • @KoVadim is an extra iteration with the calculation, then taking a while. Added in response - Vasily Barbashev

    If you subtract from the maximum value the current value of the counter ( as advised by @KoVadim ), it will also be displayed in the reverse order

     for ($i = 0; $i < 5; $i++) { echo (4-$i).'<br/>'; } // 4-0 = 4 // 4-1 = 3 // 4-2 = 2 // 4-3 = 1 // 4-4 = 0