Home >Backend Development >PHP Problem >What does php yield mean?

What does php yield mean?

coldplay.xixi
coldplay.xixiOriginal
2020-07-02 09:23:144980browse

php yield means that the yield keyword returns data from the generator function. The yield statement looks a lot like the return statement. Yield does not stop the execution of the function and return, but provides a loop generator code. value, and pauses the execution of the generator function.

What does php yield mean?

php yield means:

1. What is yield?

The yield keyword returns data from the generator function:

The core of the generator function is the yield keyword. In its simplest form, the yield statement looks a lot like a return statement. Rather than stopping the execution of a function and returning, yield provides a value to the loop generator's code and pauses execution of the generator function.

2. What is a generator function?

Generator functions are actually a more compact and efficient way of writing iterators. It allows you to define a function (your xrange) that will

calculate and return_ values ​​as you iterate over the function:

foreach (xrange(1, 10) as $key => $value) {
    echo "$key => $value", PHP_EOL;
}

This will create the following output:

0 => 1
1 => 2
…
9 => 10

You can also control $key in foreach using

yield $someKey => $someValue;

in the generator function, $someKey is what you want to display The contents of $key and $someValue are the values ​​in $val. In the question's example it's $i.

3. How is it different from normal function?

Now you may be wondering why we didn't just use PHP's native range function to achieve this output. Yes you are. The output will be the same. The difference is how we get there.

When we use rangePHP, it will execute it, create an entire array of numbers in memory, and return that entire array

to a foreach loop that will then go over it and output the value. In other words, foreachwill will operate on the array itself. The range function and foreach only "talk" once. Think of it like getting a package through the mail. The delivery person will hand the package to you and leave. Then unpack the entire package and remove whatever is inside.

When we use a generator function, PHP will step into the function and execute until it encounters the end or yield keyword. When yield is encountered, it will return the current value to the outer loop. It then returns to the generator function and continues from where it was generated. Since your xrange has a for loop, it will execute and yield until $max is reached. Think of it as foreach and the generator playing ping pong.

4. Why do I need that?

Obviously, generators can be used to work around memory constraints. Depending on your environment, doing range(1, 1000000) will make your script fatal, while doing the same with generators will be fine. Or as Wikipedia states:

Since generators only compute their yield values ​​on demand, they are useful for representing sequences that are expensive or cannot be computed immediately. These include, for example, infinite sequences and real-time data streaming.

The generator should also be fast. But remember, when we talk about fast, we're usually talking in very small terms. So before you start running and changing all your code to use a generator, benchmark to see where it makes sense.

Another use case for generators is asynchronous coroutines. The yield keyword not only returns values, but it also accepts them. For more information on this, see the two excellent blog posts linked below.

5. When can yield be used?

Generators were

introduced in PHP 5.5. Attempting to use a version of yield prior to this will result in various parsing errors depending on the code following the keyword. So if you get parsing errors from this code, please update your PHP.

Related learning recommendations: PHP programming from entry to proficiency

The above is the detailed content of What does php yield mean?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Related articles

See more