fork download
  1. <?php
  2.  
  3.  
  4. class XRange implements Iterator
  5. {
  6. protected $value = 0;
  7.  
  8. protected $limit;
  9.  
  10. protected $step;
  11.  
  12. protected $initial;
  13.  
  14. protected $key = 0;
  15.  
  16. public function __construct($value, $limit, $step = 1)
  17. {
  18. $this->value = $this->initial = $value;
  19.  
  20. $this->limit = $limit;
  21.  
  22. $this->step = $step;
  23. }
  24.  
  25. public function rewind()
  26. {
  27. $this->value = $this->initial;
  28. }
  29.  
  30. public function current()
  31. {
  32. return $this->value;
  33. }
  34.  
  35. public function next()
  36. {
  37. $this->value += $this->step;
  38.  
  39. ++$this->key;
  40. }
  41.  
  42. public function valid()
  43. {
  44. return $this->value <= $this->limit;
  45. }
  46.  
  47. public function key()
  48. {
  49. return $this->key;
  50. }
  51. }
  52.  
  53.  
  54. foreach(new XRange(1, 10, 1) as $value) {
  55.  
  56. echo $value;
  57. }
Success #stdin #stdout 0.02s 52472KB
stdin
Standard input is empty
stdout
12345678910