Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to do this.. backwards?

Tags:

loops

php

I currently have:

$i = 1;
while {
  echo $i;
  $i++;
}

And it shows:

1
2
3
4 etc..

How would I make it display backwards?

For example

4
3
2
1 etc..

I basically want to do the exact same thing but flip it around.

like image 632
Latox Avatar asked Dec 03 '10 15:12

Latox


People also ask

How is backward roll done?

Backward rollThe gymnast starts in a standing position and bends to a squat/sitting position with their arms in front. They then lower and lean back slightly until their bottom reaches the floor. They then continue this momentum and roll over their back onto their shoulders.


1 Answers

Example - Print number through 0 to 5 with PHP For Loop

for($i=0; $i<=5; $i=$i+1)
{
    echo $i." ";
}

In the above example, we set a counter variable $i to 0. In the second statement of our for loop, we set the condition value to our counter variable $i to 5, i.e. the loop will execute until $i reaches 5. In the third statement, we set $i to increment by 1.

The above code will output numbers through 0 to 5 as 0 1 2 3 4 5. Note: The third increment statement can be set to increment by any number. In our above example, we can set $i to increment by 2, i.e., $i=$i+2. In this case the code will produce 0 2 4.

Example - Print number through 5 to 0 with PHP For Loop

What if we want to go backwards, that is, print number though 0 to 5 in reverse order? We simple initialize the counter variable $i to 5, set its condition to 0 and decrement $i by 1.

for($i=5; $i>=0; $i=$i-1)
{
    echo $i." ";
}

The above code will output number from 5 to 0 as 5 4 3 2 1 0 looping backwards.

Good luck! :)

like image 151
rofyg Avatar answered Sep 27 '22 23:09

rofyg