Jump to content

[SOLVED] Loop


Recommended Posts

Hey all,

 

Got a little problem, I have the code below :

 

<?php

  foreach (range('0000','9999') as $num) {

    echo "  $num <br />";
  }
?>

 

Work's but hasnt done what i expected it to do,

 

what i want is for it to loop through :

 

0001

0002

0003

 

9998

9999

 

etc ... ... but it doesnt it starts at 1 and not 0001 any one got any ideas?

 

 

Link to comment
https://forums.phpfreaks.com/topic/104734-solved-loop/
Share on other sites

Well, if it always starts at 0, it will be $num + 1 once it's done:

<?php
  foreach(range(0,9999) as $num){
    echo "  ".str_pad($num,4,'0',STR_PAD_LEFT)." <br />";
  }
  echo "It ran ".($num + 1)." times";
?>

 

if you don't know...just use a counter:

<?php
  $i = 0;
  foreach(range(0,9999) as $num){
    echo "  ".str_pad($num,4,'0',STR_PAD_LEFT)." <br />";
    $i++;
  }
  echo "It ran $i times";
?>

Link to comment
https://forums.phpfreaks.com/topic/104734-solved-loop/#findComment-536102
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.