What does yield mean in PHP?

php yield from
php yield async
php yield return type
php generator::next
php yield empty
php generator class
generateur php
yield statement

I've recently stumbled over this code:

function xrange($min, $max) 
    for ($i = $min; $i <= $max; $i++) {
        yield $i;

I've never seen this yield keyword before. Trying to run the code I get

Parse error: syntax error, unexpected T_VARIABLE on line x

So what is this yield keyword? Is it even valid PHP? And if it is, how do I use it?

What does yield mean in PHP?, In its simplest form, a yield statement looks much like a return statement, except that instead of stopping execution of the function and returning, yield instead provides a value to the code looping over the generator and pauses execution of the generator function. When we use the generator function, PHP will step into the function and execute it until it either meets the end or a yield keyword. When it meets a yield, it will then return whatever is the value at that time to the outer loop. Then it goes back into the generator function and continues from where it yielded.

This function is using yield:

function a($items) {
    foreach ($items as $item) {
        yield $item + 1;

is almost the same as this one without:

function b($items) {
    $result = [];
    foreach ($items as $item) {
        $result[] = $item + 1;
    return $result;

The only one difference is that a() returns a generator and b() just a simple array. You can iterate on both.

Also, the first one does not allocate a full array and is therefore less memory-demanding.

Generator syntax - Manual, The heart of a generator function is the yield keyword. The syntax for yielding a key/value pair is very similar to that used to define an associative array,  Woman: "I can't breathe!". Woman: "I yield!". At which point, the woman falls at the man's feet, and the man proceeds to take her sexually, usually vaginally. The word usage implies that the man does the work of removing her clothes, but that she does the work of spreading her legs.

How to use the "yield" keyword in PHP 5.5 and up, 5.5. This keyword allows you to program “generators”. A generator is very similar to a function that returns an array, in that a generator has parameters, can be called, and generates a sequence of values. The “yield” keyword is new in PHP 5.5. This keyword allows you to program “generators”. Wikipedia explains generators accurately: A generator is very similar to a function that returns an array, in that a generator has parameters, can be called, and generates a sequence of values.

simple example

echo '#start main# ';
function a(){
    echo '{start[';
    for($i=1; $i<=9; $i++)
        yield $i;
    echo ']end} ';
foreach(a() as $v)
    echo $v.',';
echo '#end main#';


#start main# {start[1,2,3,4,5,6,7,8,9,]end} #end main#

Generators overview - Manual, What is a generator and how is it used in PHP? The yield of a property is often used when buy-to-let investors are considering the purchase price of a property versus how much they might be able to rent it out for. They can then decide if they believe their funds may perform better elsewhere, such as in a savings account.

Deal with Memory Gently using “Yield” in PHP - tajawal, foreach to iterate over a set of data without needing to build an array in memory, which may cause you to exceed a memory limit, or require a considerable amount of processing time to generate. Yield, submit, surrender mean to give way or give up to someone or something. To yield is to concede under some degree of pressure, but not necessarily to surrender totally: to yield ground to an enemy.

PHP Generators From Scratch – Alan Storm, I will answer the question “What is the benefit of having yield in PHP” Generator class is implementing Iterator interface, which means that you have to loop  Yield definition is - to bear or bring forth as a natural product especially as a result of cultivation. How to use yield in a sentence. Synonym Discussion of yield.

What does yield mean in PHP?, The "yield" keyword is new in PHP 5.5. It allows I wrote a small script in PHP that shows how to do this (efficiently) using the “yield” keyword:  yield (Science: botany) standing crop expressed as a rate, i.e., grams dry weight per metre square per day.

Understanding PHP Generators ― Scotch.io, Alternative to linear code flow — what does that even mean? PHP will always treat a function that includes the yield keyword as a generator  Yield refers to the earnings generated and realized on an investment over a particular period of time, and is expressed in terms of percentage based on the invested amount or on the current market value or on the face value of the security.

  • Please elaborate as to what the benefits of yeild are over, say, a solution like this one: ideone.com/xgqevM
  • Ah, well, and the notices I was generating. Huh. Well, I experimented in emulating Generators for PHP >= 5.0.0 with a helper-class, and yeah, slightly less readable, but I may use this in the future. Interesting topic. Thanks!
  • Not readability but memory usage! Compare used memory for iterating over return range(1,100000000) and for ($i=0; $i<100000000; $i++) yield $i
  • @mike yes, that's already explained in my answer though. In the other Mike's example memory is hardly an issue because he's only iterating 10 values.
  • @Mike One problem with the xrange there is that its use of statics limits is usefulness for nesting for instance (For instance searching over an n dimensional manifold, or a recursive quicksort using generators, for instance). You can't nest xrange loops because theres only a single instance of its counter. The Yield version does not suffer this problem.
  • addt notes from the official docs: In PHP 5, a generator could not return a value: doing so would result in a compile error. An empty return statement was valid syntax within a generator and it would terminate the generator. Since PHP 7.0, a generator can return values, which can be retrieved using Generator::getReturn(). php.net/manual/en/language.generators.syntax.php
  • Simple and concise.
  • I didn't understood how generators work. this class implements the iterator interface. from what I know the iterators classes allow me to configure how I want to iterate over an object. for example the ArrayIterator gets an array or object so I can modify values and keys while iterating it. so if iterators get the whole object/array then how does the generator doesn't have to build the whole array in the memory???
  • So, it returns without interrupting the function?