Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

252
Views
PHP generator function explained

I am trying to get the logic of php generator functions, what i got so far is that is that a generator function is exactly like a normal function but you can use the keyword yield as many times as it needs to in order to provide the values to be iterated over. and this cannot be done by the return keyword .

is it like returning an array ?

where do we need generators ?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

A generator allows you to loop over a sequence of values without first creating an array to hold them all. This is useful if the sequence never ends (you use break to get out of the loop when some criteria is reached) or if it would take a long time or use lots of memory to create all the array elements.

Instead of collecting all the values into an array, the generator calculates each value as it's needed, and uses yield to produce this value.

There's an example in the documentation showing a variation of the range() function that uses a generator, so you can process very large ranges without using lots of memory.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!