Today I Learned Something About Iterators In PHP

Wouter wearing a monster cap
Wouter van Marrum
Shared image with code example of what I've learned.

When you use the method `iterator_count` within PHP you can not use the iterator in a loop anymore because it will basically consume the iterator so you cannot use it anymore.

See the following example:

```php

<?php

$pagesIterator = $this->pageRepository->getQuery()->toIterable();

$this->logger->info('Pages: Amount of pages to run on ' . iterator_count($pagesIterator));

foreach ($pagesQuery as $page) { // Will trigger error

```

In order to resolve this issue you could do something like the code below:

```php

<?php

$pagesQuery = $this->pageRepository->getQuery();

$this->logger->info('Pages: Amount of pages to run on ' . iterator_count($pagesQuery->toIterable()));

foreach ($pagesQuery->toIterable() as $page) {
// Do some stuff here.
}

```