Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

255
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda