srph / map-range
A more efficient foreach-range
v0.1.0
2015-08-26 19:11 UTC
Requires (Dev)
- phpunit/phpunit: ^4.8
This package is not auto-updated.
Last update: 2024-12-25 11:20:21 UTC
README
composer require srph/map-range
A more efficient foreach
-range
.
Usage
SRPH\MapRange\map_range(function($index) { // do something }, $from, $to);
For PHP >=v5.6, you can use the use function (aka import function) syntax:
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
foreach(range(0, 10) as $i) { // .. }
Over this:
for ( $i = 0; $i < 10; $i++ ) { // }
In which the first example generates a buffer array first (which is terrible for performance).