Use PHP Generators in improving the performance of web applications

Hi everyone, as we know, the speed of a website is directly related to the user experience. It makes the difference between a good web application and a perfect web application. Therefore, in the position of a developer, we always want to find ways to improve the performance of the products we make. Is it a bit rambling, we're talking about PHP Generators, why talk about performance and speed here. Well, in many ways to improve the performance of a website better, today I would like to introduce you to a method of optimizing performance directly in the code. That is using PHP Generators . Let's start together.

What is PHP Generators?

Starting in version 5.5, PHP provides us with a new way to access data in an array without storing all of the array elements in memory space called Generators. Is it still a little confusing? We will go through the following example to better understand this issue.

First, on our server we need to create a file called generator.php and we will use it throughout this article. After creating the file, let's add some code lines as follows:

Then let's start the server that has installed PHP

Start the browser and go to http: // localhost: 8000 / generator.php we will see the results of the above program as follows:


Oh, it seems like things haven't been very unstable until a nice day we try to initialize our getRange () function with a very large number. For example, change in foreach () as follows:

Now we see the getRange function (PHP_INT_MAX) passed as the maximum integer value that version PHP currently provides. Try reruning the browser, we will see an error message like this:


The above message tells us that our PHP server is running out of memory. One solution to this problem is to edit the php.ini file to increase the memory_limit parameter . But let us wonder if it really has to be the optimal solution, obviously we are seeing a problem here is that only a small piece of code can occupy your memory resources. That is really an unacceptable thing. So what is the solution to this problem? We'll find out in the next section.

Using PHP Generators

Now we try to redefine the above getRange () function using the Generators as follows:

Now let's take a look at the getRange () function, we just loop through the values ​​of the variable $ i and return the result with the yield keyword . In a simple way, we can understand the yield has the same function as return is returning the value of a function. However yield returns the value when the value needs to be used without storing all values ​​in memory. After rewriting the above function, we can rerun the browser and see the results.


There was no memory overflow error, in a while allowing the browser to display the results as we expected. It can be seen that PHP Generators themselves play an important role in saving memory space.

Note: A small note is that we can only use generators in a function

Why should use Generators

In many cases we want to handle a very large data set (such as data from system log files) or want to calculate processing on an array with extremely large elements. Obviously we don't want to handle the data sets that take up a large amount even of our memory and what we should do is find ways to save as much memory as possible. Instead of having to store a large data set, we can use the generators to retrieve what data is needed.

Returning Keys

In some cases, our data is really meaningful when expressed as key => value and while using generators we can also return that form in the following way:

We can then use the key => value value pair as follows:

Parameter transmission and in Generators

Suppose we want to add a few parameters to specify how the Generators work, such as adding stop conditions for Generators . We do it as follows:

And to add the value injected into the getRange () function, we do the following:

Note: We can use the return keyword to end all processing in Generators


Generators provide a powerful solution for saving server performance, thus making server processing lighter, which means we don't necessarily have good hardware to speed up. degree of the website. However, it should not be overused because it will likely cause more serious problems for your system if used incorrectly.

ITZone via Viblo

Share the news now