0

I don't understand yield PHP very well.... all the examples online are not useful examples.

For example in my web app, actually, to create a GUI of results get by a function I use:

<?php

function get_results_from_database($id) {

 //connect to database

 //omitted

 return $array_results; //it is an array

}

Now my html page is like it:

<html>
<body>
<div>
<?php

 $res = get_results_from_database(5); // for example it returns 1.000 rows
 foreach ( $res as $index => $row ) {
   echo '<p>' . $row['Name'] . '</p>';
 }

 echo '<p>TOTAL ROWS: ' . count($res) . '</p>';

?>
</div>

A code like this can be converted with a yield approach? I don't understand the sense of it.... if the yield is used to async request, no sense to use it for creating graphical HTML pages...or I missing some things?

Where I can find a useful example of a yield approach?

Mujahid Bhoraniya
  • 1,518
  • 10
  • 22
Giuseppe Lodi Rizzini
  • 1,045
  • 11
  • 33
  • There's a pretty thorough answer on yield in [this question](https://stackoverflow.com/questions/17483806/what-does-yield-mean-in-php). In short, it isn't meant for any specific task by nature, it is meant for tasks with high memory demand. – El_Vanja Mar 06 '20 at 11:22
  • @El_Vanja already found this topic but not very clear to me. The part i don't understand is: if it is async...how to use is to build a html code? for build it I need sync/blocking functions...or not? – Giuseppe Lodi Rizzini Mar 06 '20 at 11:25
  • Not using / using `yield` is like going to the shop to buy some food. Without `yield` you buy everything you need and bring home __at once__, but this can be a __heavy__ load and sometimes you cannot manage with it. Using `yield` is like making several walks to the shop - you come to the shop, take little pack of of food, bring home, go to shop again, bring, go to shop, bring, until all your food is at home. – u_mulder Mar 06 '20 at 11:32
  • @u_mulder I understand sense of it....but.... if my function in example (get_results_from_database) is yielded...so this became async...i'm wrong? So if it aync and it needs 5seconds to retrieve all data.... my foreach to print "Name" wait for $res or it is executed immediately (cause get_results_from_database became async) with the risk that $res is not ready...? Any chance to see my original code converted to yield to understand better? – Giuseppe Lodi Rizzini Mar 06 '20 at 11:49
  • 1
    Where did you take the word __async__? – u_mulder Mar 06 '20 at 12:21
  • Does this answer your question? [What does yield mean in PHP?](https://stackoverflow.com/questions/17483806/what-does-yield-mean-in-php) – solid.py Mar 06 '20 at 12:25
  • It's unclear what you think yield has to do with asynchronous execution. Please clarify what you think is supposed to happen. The primary purpose of yield, and generator functions, is (according to the PHP manual) to reduce memory usage. – ADyson Mar 06 '20 at 12:49
  • Also, AFAIK for it to actually have any effect, you would have to call the function from within your loop construction, e.g. `foreach ( get_results_from_database(5) as $index => $row ) {`. Because if you call the get_results_from_database(5) first, and assign the result to $res, it has to build the whole result at once and return it, before your loop starts. So it cannot take advantage of yield's ability to give the results to the loop one at a time. – ADyson Mar 06 '20 at 12:55
  • @ADyson hi, actually I see for the first time yield in telegram bot functions.... as a manual of these bot said, yield is used for async calls.... so in my mind now have an idea like $.ajax({async:true}) where the returned value is async and if I need to manipulate it outside $.ajax call, I can't.... – Giuseppe Lodi Rizzini Mar 06 '20 at 13:12
  • No I think it's a completely different concept. Not sure this manual you read really used the word "async" appropriately, although I would need to see the manual and read it in context to be sure - is it something which is published online? And I'm still unclear precisely what effect you'd be expecting such behaviour, if it was "async", to have on the code execution and output? Did you think it would execute each iteration of the loop asynchronously? If it did, that would be chaos because you could not guarantee the order of the output. Anyway that isn't what it does at all. – ADyson Mar 06 '20 at 13:16
  • @ADyson of course: https://docs.madelineproto.xyz/docs/ASYNC.html#enabling-the-madelineproto-async-api – Giuseppe Lodi Rizzini Mar 06 '20 at 13:18
  • They are using it along with _functions which themselves execute asynchronously in a non-blocking manner_ (e.g. things which do I/O-bound work such as HTTP requests across a network). The yield is used the content of their "event loop" functionality, so basically it allows the event loop to read the next thing which happened. The async part would only be applicable to your example above if you were able to make an asynchronous call to your database. I think perhaps it's unfortunate you came across such a complex example of yield before you really understood the basics of generator functions. – ADyson Mar 06 '20 at 13:24
  • But as I say, you can still take advantage of yield itself. Your get_results_from_database function could call the database, fetch the rows and yield each row as it fetches it (rather than adding the row to an array which is then returned at the end of the function). If you call that function from within the foreach loop as I suggested earlier, it will enable the loop to echo each value as it's yielded, rather than waiting for the whole array to return. So it will a) not use as much memory, and b) might be a bit faster. – ADyson Mar 06 '20 at 13:27
  • This simple example: https://stackoverflow.com/a/39747553/5947043 demonstrates the concept nicely using some basic echo commands. – ADyson Mar 06 '20 at 13:28
  • @ADyson ok i'll try to do some test with simple script to check if I undestand how it works and if I get real benefits using this yield. Thanks – Giuseppe Lodi Rizzini Mar 06 '20 at 15:15
  • Probably unless you have hundreds of rows taking up a lot of RAM, you might not see huge benefits. The other benefit area is the async stuff which that telegram example has taken advantage of - because it allows them to see a result after one async call has completed, rather than waiting for them all to be done. – ADyson Mar 06 '20 at 15:16

0 Answers0