

Later in the code, the iteration is performing two ways to. Print( "The yeilded values of the generator function by iterator_to_array are : \n ") Īs in the above program, the gen_even_numbers() function is created to find and return the even numbers in between 2 to 10, and then another function call_gen() is used to call gen_even_numbers() function by using yield from, which does not reset the keys. Next, we write the PHP code to understand the PHP yield keyword, where the yield keyword is used to yielding multiple values from the generator function, and that generator function is calling inside another generator function, as below –

In each iteration, it returns the values one by one and which are displaying as well, as we can see in the output.

Next, we write the PHP code to understand the PHP yield keyword more clearly with the following example, where the yield keyword is used to yielding multiple values from the generator function, as below –Īs in the above program, the gen_even_numbers() function is created to find the even numbers between 2 to 10 and yield or return when it is calling iteratively. Examples of the PHP yield keywordĭifferent examples are mentioned below: Example #1 – yielding multiple values When there are no values to yield, then the generator simply exits, and the calling code can continue just as the array run out of values. When we iterate this object by a foreach loop, which calls the object’s iteration methods by PHP each time it needs a value, then the state of the generator function is saved when the generator function yields a value that is resumed to get the next value. When the generator function is called, it returns an iterator object which can be iterated.

The PHP yield keyword uses in the generator function to returns or yields the multiple values. Return value – The return value of this method is the values required to be returned in the generating function. Value – This is not optional, that specifies the value which is to be returned. Web development, programming languages, Software testing & others Start Your Free Software Development Course
