{"id":808,"date":"2017-03-29T16:41:47","date_gmt":"2017-03-30T00:41:47","guid":{"rendered":"http:\/\/www.tech.dimprash.com\/?p=808"},"modified":"2017-03-29T16:41:47","modified_gmt":"2017-03-30T00:41:47","slug":"php-generators","status":"publish","type":"post","link":"http:\/\/www.tech.dimprash.com\/?p=808","title":{"rendered":"PHP Generators"},"content":{"rendered":"<p>Generators are functions that provide a simple way to loop through data without the need to build an array in memory. &#8220;yield&#8221; is the main command here. <\/p>\n<pre>\r\nfunction getRange ($max = 10) {\r\n    for ($i = 1; $i < $max; $i++) {\r\n        yield $i;\r\n    }\r\n}\r\n\r\nforeach (getRange(PHP_INT_MAX) as $range) {\r\n    echo \"Dataset {$range} <br>\";\r\n}\r\n<\/pre>\n<p>Dissecting the getRange function, this time, we only loop through the values and yield an output. yield is similar to return as it returns a value from a function, but the only difference is that yield returns a value only when it is needed and does not try to keep the entire dataset in memory.<\/p>\n<p>Why we use generators?<br \/>\nThere are times when we might want to parse a large dataset (it can be log files), perform computation on a large database result, etc. We don&#8217;t want actions like this hogging all the memory. We should try to conserve memory as much as possible. The data doesn&#8217;t necessarily need to be large \u2014 generators are effective no matter how small a dataset is.<\/p>\n<p>Generators can be key-value too : <\/p>\n<pre>\r\nfunction getRange ($max = 10) {\r\n    for ($i = 1; $i < $max; $i++) {\r\n        $value = $i * mt_rand();\r\n\r\n        yield $i => $value;\r\n    }\r\n}\r\n<\/pre>\n<p>Generators can also take in values. This means that generators allow us to inject values into them. For example, we can send a value to our generator telling to stop execution or change the output.<\/p>\n<pre>\r\nfunction getRange ($max = 10) {\r\n    for ($i = 1; $i < $max; $i++) {\r\n        $injected = yield $i;\r\n\r\n        if ($injected === 'stop') return;\r\n    }\r\n}\r\n\r\n$generator = getRange(PHP_INT_MAX);\r\n\r\nforeach ($generator as $range) {\r\n    if ($range === 10000) {\r\n        $generator->send('stop');\r\n    }\r\n\r\n    echo \"Dataset {$range} <br>\";\r\n}\r\n\r\n<\/pre>\n<p><a href=\"https:\/\/scotch.io\/tutorials\/understanding-php-generators\">https:\/\/scotch.io\/tutorials\/understanding-php-generators<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Generators are functions that provide a simple way to loop through data without the need to build an array in memory. &#8220;yield&#8221; is the main command here. function getRange ($max = 10) { for ($i = 1; $i < $max; $i++) { yield $i; } } foreach (getRange(PHP_INT_MAX) as $range) { echo \"Dataset {$range} \"; &hellip; <a href=\"http:\/\/www.tech.dimprash.com\/?p=808\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">PHP Generators<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-808","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts\/808","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=808"}],"version-history":[{"count":1,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts\/808\/revisions"}],"predecessor-version":[{"id":809,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=\/wp\/v2\/posts\/808\/revisions\/809"}],"wp:attachment":[{"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=808"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=808"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.tech.dimprash.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=808"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}