Skip to content

Instantly share code, notes, and snippets.

@demaisj
Created September 3, 2013 10:00
Show Gist options
  • Save demaisj/6421932 to your computer and use it in GitHub Desktop.
Save demaisj/6421932 to your computer and use it in GitHub Desktop.
PHP : Improved native for loop variations for much speed when iterating
<?php
$array = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
for($i = count($array)-1;$i >= 0; $i--) {
echo $array[$i].", ";
};
/* Will produce :
10, 9, 8, 7, 6, 5, 4, 3, 2, 1,
*/
<?php
$array = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
for($i = count($array)-1;$i >= 0; $i--) {
$n = count($array)-1-$i;
echo $array[$n].", ";
};
/* Will produce :
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment