https://github.com/srph/map-range
:curly_loop: A more efficient foreach-range
https://github.com/srph/map-range
Last synced: 3 months ago
JSON representation
:curly_loop: A more efficient foreach-range
- Host: GitHub
- URL: https://github.com/srph/map-range
- Owner: srph
- License: mit
- Created: 2015-08-26T18:47:43.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2015-08-26T19:35:07.000Z (almost 11 years ago)
- Last Synced: 2025-01-26T17:15:44.868Z (over 1 year ago)
- Language: PHP
- Homepage:
- Size: 152 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE.md
Awesome Lists containing this project
README
# map-range [](https://travis-ci.org/srph/map-range?branch=master) [](https://packagist.org/packages/srph/map-range) [](https://packagist.org/packages/srph/map-range) [](https://packagist.org/packages/srph/map-range) [](https://packagist.org/packages/srph/map-range)
```
composer require srph/map-range
```
A more efficient `foreach`-`range`.
## Usage
```php
SRPH\MapRange\map_range(function($index) {
// do something
}, $from, $to);
```
For PHP >=v5.6, you can use the use function (aka import function) syntax:
```php
use function SRPH\MapRange\map_range;
map_range(function($index) {
// do something
}, $from, $to);
```
Note that it iterates *while* `$from <= $to`.
## Why
Because some developers prefer this
```php
foreach(range(0, 10) as $i) {
// ..
}
```
Over this:
```php
for ( $i = 0; $i < 10; $i++ ) {
//
}
```
In which the first example generates a buffer array first (which is terrible for performance).