<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/bhjecbtcz" -->

---
title: Why I prefer array functions over loops | daily.dev
description: The author prefers using array functions over `foreach` loops in PHP due to clearer communication of intent, encapsulation of functionality within the...
canonical: https://daily.dev/posts/bhjecbtcz
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Why I prefer array functions over loops | daily.dev
og:description: The author prefers using array functions over `foreach` loops in PHP due to clearer communication of intent, encapsulation of functionality within the...
og:url: https://daily.dev/posts/bhjecbtcz
og:image: https://api.daily.dev/og/posts/bHJecBtcZ.png
og:image:alt: Post cover image
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Why I prefer array functions over loops

**[PHP Dev](https://daily.dev/sources/phpdev)** · [@backendtea](https://daily.dev/backendtea) · 17 upvotes · 9 comments

## Summary

The author prefers using array functions over `foreach` loops in PHP due to clearer communication of intent, encapsulation of functionality within the function, and reduced chances of side effects. Array functions like `array_map`, `array_filter`, and `array_reduce` are highlighted for their clarity and maintainability. While the author acknowledges that loops can be more efficient in certain cases, especially with large datasets or when manipulating multiple variables, array functions generally provide more readable and less error-prone code.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://backendtea.com/post/functions-over-loops/>

## Community discussion

Top comments from developers on daily.dev.

**@jdde** · 1 upvotes

> I definitely agree with it - for big teams, where people exist doing PHP only all day long. When you have engineers jumping between languages especially in PHP full stack setups, I would prefer the loop, to just get faster into it and see easier whats happening there.

**@noricode** · 0 upvotes

> NOTHING will be faster than a regular for() loop

**@mykytanikitenko42** · 0 upvotes

> Why are these functions part of the language itself?
> Doesn't PHP have iterators with lazy evaluation?

**@ravavyr** · 0 upvotes

> always use basic "for" loops...they're faster than all the other functions in both PHP and Javascript anyway. forEach is indeed slower too.
> basic for loops are also easier to read and understand in the long term. so future-you will thank you.

---

Tags: [#webdev](https://daily.dev/tags/webdev), [#php](https://daily.dev/tags/php)

[View this post on daily.dev](https://daily.dev/posts/bhjecbtcz)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"DiscussionForumPosting","mainEntityOfPage":"https://daily.dev/posts/bhjecbtcz","headline":"Why I prefer array functions over loops","text":"Shared: Why I prefer array functions over loops","url":"https://daily.dev/posts/bhjecbtcz","datePublished":"2024-08-12T07:54:57.454Z","dateModified":"2024-08-12T07:55:25.766Z","author":{"@type":"Person","name":"Gert","url":"https://daily.dev/backendtea","image":"https://lh3.googleusercontent.com/a/ACg8ocKHd8JY8lMDSQJxnY617wdw8F7M77hfzqPePsjAtQWu944UAQ=s96-c","interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"EndorseAction"},"userInteractionCount":200}},"image":"https://media.daily.dev/image/upload/s--OHB84bZF--/f_auto/v1722860399/public/Placeholder%2010","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":17},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":9}],"sharedContent":{"@type":"WebPage","url":"https://api.daily.dev/r/A2HPcKXr6"},"comment":[{"@type":"Comment","text":"I definitely agree with it - for big teams, where people exist doing PHP only all day long. When you have engineers jumping between languages especially in PHP full stack setups, I would prefer the loop, to just get faster into it and see easier whats happening there.","datePublished":"2024-08-12T11:49:29.649Z","url":"https://daily.dev/posts/bHJecBtcZ#c-wMUNQaYfg","author":{"@type":"Person","name":"Jörn Depenbrock","url":"https://daily.dev/jdde","image":"https://avatars.githubusercontent.com/u/10098106?v=4"},"interactionStatistic":{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":1}},{"@type":"Comment","text":"NOTHING will be faster than a regular for() loop","datePublished":"2024-10-21T15:52:39.921Z","url":"https://daily.dev/posts/bHJecBtcZ#c-a6cDVdjPp","author":{"@type":"Person","name":"noricode","url":"https://daily.dev/noricode","image":"https://media.daily.dev/image/upload/v1672934357/avatars/avatar_v9I0Ty3qOPTDx9IvWetC0.jpg"}},{"@type":"Comment","text":"Why are these functions part of the language itself?\nDoesn’t PHP have iterators with lazy evaluation?","datePublished":"2024-09-16T18:42:53.727Z","url":"https://daily.dev/posts/bHJecBtcZ#c-3KtIQR0xo","author":{"@type":"Person","name":"Mykyta Nikitenko","url":"https://daily.dev/mykytanikitenko42","image":"https://lh3.googleusercontent.com/a/AAcHTtfurSXqjGczJrC4WFT2LrMQYjGe-Qrau5HTeaP1=s96-c"}},{"@type":"Comment","text":"always use basic “for” loops…they’re faster than all the other functions in both PHP and Javascript anyway. forEach is indeed slower too.\nbasic for loops are also easier to read and understand in the long term. so future-you will thank you.","datePublished":"2024-09-05T20:28:55.129Z","url":"https://daily.dev/posts/bHJecBtcZ#c-zD0JdOhDx","author":{"@type":"Person","name":"Ramsez Stamper","url":"https://daily.dev/ravavyr","image":"https://lh3.googleusercontent.com/a/AGNmyxYvk0A28pTmE32gmh59XDuOpMsFjjhwtMm6TiszOQ=s96-c"}}],"isPartOf":{"@type":"WebPage","url":"https://daily.dev/squads/phpdev","name":"PHP Dev"}}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"PHP Dev","item":"https://daily.dev/squads/phpdev"},{"@type":"ListItem","position":3,"name":"Why I prefer array functions over loops"}]}
```

