# How to Use Yield and Generators in PHP

I've written multiple tutorials about functions in PHP. These include an overview of basic concepts like function return values and parameters and anonymous functions in PHP.

One common feature of all the functions that we defined in those two tutorials is that they had a single return value. Functions in PHP always return a single value and don't execute any code that comes after the return statement—unless they are generator functions!

In this tutorial, we will delve a bit deeper into the topic of functions in PHP and learn about the yield keyword and generator functions in PHP.

## The Yield Keyword and Generator Functions in PHP

The word "yield" means "produce" or "generate", which is basically what the keyword does in PHP. It generates a value for use outside your function without entirely stopping its execution and returning. It gives you the ability to pause the execution while you do something else with the returned value. Functions which use the yield keyword are called generator functions.

A yield statement looks similar to the return statement, but it's different in the way it behaves. The following example will help you understand the difference easily.

## Generator Delegation

Generator delegation was implemented in PHP 7.0. This allows you to yield values from another generator, array, or Traversable object within your outer generator function. One of the advantages of using generator delegation is that it allows you to refactor your code and improve readability. Here's an example that creates two different types of data inside a generator function yielded by other generator functions.

In the above example, we have a generate_string() function that outputs a random string of 16 characters. The check_name() function is our generator that yields a string which contains our specified name. It then goes on to yield the number of attempts. The divide_in_parts() function is our other generator function that yields three different combinations of numbers that add up to a given number.

We run a for loop inside our main_function() which accepts an array of mixed values. After that, we use yield from with our two functions alternately.

There are two things worth keeping in mind here. First, the functions yield all the values that they can in each run. This is why the check_name() function outputs two values and divide_in_parts() outputs three values. Second, the keys start from zero with each call to either of these functions. Expecting the keys to increase with every call might give you strange results.

## Final Thoughts

This tutorial introduced you to some basic concepts related to yield and generators in PHP. You should now have a better understanding of the purpose of the yield keyword and generator functions and how to define your own generator functions.

We've also covered some advantages of using generators like low memory usage, the ability to pause function execution, and generating values for consumption on demand. The next sections discussed how to use generators to yield values by reference or to produce key-value pairs. Finally, we moved on to generator delegation, which can help you write better and more readable code.