Understanding the PHP generators for beginners

Understanding the PHP generators for beginners

Understanding the PHP generators is not a big issue even for the beginners. I hope you will get the complete idea after reading this article. So, let’s start.

What are PHP Generators?

We all know that the memory management is a worst deal in all the programming plate forms. A small, light weighted and fast application always loved by the people. In PHP 5.5, we have the concept of PHP generators. This is very useful concept because with this concept you can speed up our applications. Basically, this concept does not build an array in memory. Let me explain with an example first. Create a file generator. php in your localhost, paste the following code, execute the code and see the magic.

php generators-output

I am sure above code is not so difficult to discuss. Also the output of the code. Ok! Now, update the upper code with this.

We know that PHP_INT_MAX is the largest number that your running PHP version can reach. After updating the code refresh the browser. This time it will generate the fatal/warning error.

php generators
What happened?

PHP ran out of memory? I am going to php.ini for increasing the memory_limit. Is that? No way?


Hold on! Because now, with php generators, we can control this issue. Let’s see how it will be possible.

Using PHP Generators:

Reuse the same function above and call PHP_INT_MAX. This time we are creating a generator function.

Note: Before going to explain, keep in mind that generators can only be used from a function.

So, discussing the getLimit, here is a function, a loop and the new thing “yield”. It is used for output similar to “return”. But it is also different form return because it does not keep the complete dataset in memory and returns the value on demand. Refresh your browser. This time it will produce result but after processing for some time.

Why We Use Generators?

Actually, we all know that aim is to make our application faster but consuming less memory. We must try to conserve memory. Memory management the main and burning reason for using generators.

Returning Keys:

Generators can yield key-value pairs that was not possible before. Check the below code.

The above code is making key-value pairs and that can be used with an array like this.

How to send values to generators?

Generators have ability to inject values into them. For example, generator can accept values with as a command to stop execution of output. Use getLimit function above,

This value can be injected as

Did you noticed? We are using return and yield in a same function. The reason is that return breaks out the generator function.

Never misuse the Generators

Commonly, generators are used for memory management but it does’nt mean that they are the ultimate solution. They can also create the problem and problem can also be doubled if misused.
But still we can’t deny its significant performance. Make a little refactoring and try to implement.