DEV Community

Discussion on: Which types of loops are most popular in the programming languages you use?

Collapse
 
chrisrhymes profile image
C.S. Rhymes

I always used to use foreach in php, but now I’ve swapped to using collection methods in Laravel as they are much more powerful and you can chain them together instead of having multiple loops or nested loops.

Here is an example of map, taken from the docs, which I use a lot

$collection = collect([1, 2, 3, 4, 5]);

$multiplied = $collection->map(function ($item, $key) {
    return $item * 2;
});

$multiplied->all();

// [2, 4, 6, 8, 10]