What is the difference between 'Yield' and 'Return' in PHP?

Asked

Viewed 4,752 times

13

I have been trying to remove this doubt, but without success. At first glance they seemed to me similar commands or they would be the same thing. After seeing some explanations, I was a little confused and did not take my doubt.

If they are not the same thing, I would like if possible, an example that could show the use of one and the other just to make clearer the difference between them.

  • 1

    Yield is using with generators and is available from php5.5 forward, the Return is there since always usually used in functions a few times out of them, single same ...

  • http://answall.com/q/50208/91

  • @Rray, I saw this post and I even commented on it in the reply of the bigown, but I didn’t get anything in it that could get me that doubt. At least I didn’t see anything that could differentiate one from the other.

  • 2

    It was also slow to understand and it’s a great question. + 1

3 answers

18


According to the php manual.

Simply put: One is about a return of a value, another is for creating a Generator.

YIELD

A Yield statement looks a lot like a return, except that instead of stopping the execution of the function and returning, Yield provides a value for the loop code over Generator and pauses the execution of the Generator function.

I’m not going to talk here about Generators, because that question explained it very well:

What are the advantages of using a Generator (Yield) in PHP?

But changing in kids, Generator is nothing less than a simplified way to apply an Iterator on a certain operation.

So when you use yield you are indicating that the value passed to it will be part of a step of the iteration you want. There will be a "pause" in the iteration and those values will be returned in a class object Generator.

Generator in turn implements an interface called Iterator in PHP. It would be good to understand its use, because in this case, it would not be important to learn only the Generator syntax before learning what is iterator.

If you know what a Iterator then we can say, When you call an object Generator with foreach, you are invoking the methods in the following sequence:

foreach (gen() as $key => $value) {

}

Amounts to:

$gen = gen();

$gen->rewind();

while ($gen->valid()) {
    $key  = $gen->key();
    $value = $gen->current();

   $gen->next();
}

RETURN

The return, that translating is "return", serves to return a value to a function. Are two different cases, since in the first case, the Generator is just a "pause" for the loop that was proposed inside. Already return represents the definitive value returned for that function call.

I can use both together?

It depends on the version of PHP you are using. If you are using versions prior to PHP 7, you will receive an exception when trying to put return and yield in the same role.

In PHP 7, you can do this. In this case, you will have to use the method Generator::getReturn.

function gen()
{
     yield 1 => 2;
     yield 3 => 4;

    return 5;
}

gen()->getReturn(); // 5

Other differences

The return can be used outside a function, already the yield can’t.

The best way to get you one generator through the yield, having the Generator object directly in a variable, it is only with the self invoking Function. However, it is only available for PHP 7.

Example:

 $gen = (function ()
 {
    yield "A" => "B";

  })();

 var_dump($gen); // Generator(object);

The yield can be processed according to the number of declarations, already the return no. You can only have more than one return in the function, but when the first one is executed, the rest will be ignored. Already the yield it’s not like that.

function hello()
{
    yield "Stack Overlow";
    yield "Mandioca";
    yield "Milho";
}

$gen = hello();
var_dump($gen->current());
$gen->next();
var_dump($gen->current());

The exit will be:

string(13) "Stack Overlow"
string(8) "Mandioca"
  • I’m still completing the answer (I’m busy).

  • 3

    yield ++$up; :D

  • 2

    Very good answer Wallace! Complete and enlightening :)

  • @Wallacemaxters finish the answer there

  • 2 years, Nussa kkkkk

7

Wallace Maxters' answer is fine, I’m just going to give a slightly different view of the same thing.

I see the yield as the cited generator/iterator. It is responsible for maintaining status between the calls of the function that use it. He is responsible for storing where he stopped in the execution of the function and therefore where he should resume when it is executed again.

Actually he has a return implicit in its execution. So it is usually used in place of the return pure that is the simple return without keeping state.

Additional information for those arriving here with total doubt on the subject:

  • I don’t know what the reason is, but in PHP 7 you can use both.

4

A simple explanation is that Return returns the value and terminates the function, Yield returns the value and gives a "pause" on the function (On the next call it continues right after Yield).

In practice one is not compatible with the other and they are used in different ways in different contexts. If you are interested, look for generating functions or iterators. If not, just always use Return and ignore Yield.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.