What does yield mean in PHP?

Forums PHPWhat does yield mean in PHP?
Staff asked 3 years ago

Answers (2)

Add Answer
Prince Dhameliya Marked As Accepted
Staff answered 3 years ago

The yield keyword is used to create a generator function. Generator functions act as iterators which can be looped through with a foreach loop.

The value given by the yield keyword is used as a value in one of the iterations of the loop.

Staff answered 3 years ago

The yield keyword is at the heart of a generator function. In its most basic form, a yield statement resembles a return statement, except that instead of halting the function and returning, yield gives the code looping over the generator a value and pauses the generator function’s execution.

To make a generator function, use the yield keyword. Generator functions are iterators that can be looped through using the foreach loop.
The yield keyword returns a value that is used as a value in one of the loop’s iterations.

Subscribe

Select Categories