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"
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 ...
– rray
http://answall.com/q/50208/91
– rray
@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.
– DiChrist
It was also slow to understand and it’s a great question. + 1
– Guilherme Nascimento