{"id":1846,"date":"2017-02-01T21:19:54","date_gmt":"2017-02-01T21:19:54","guid":{"rendered":"http:\/\/www.codingfriends.com\/?p=1846"},"modified":"2021-05-22T16:21:06","modified_gmt":"2021-05-22T15:21:06","slug":"php-7-generator-yields","status":"publish","type":"post","link":"https:\/\/www.codingfriends.com\/index.php\/2017\/02\/01\/php-7-generator-yields\/","title":{"rendered":"php 7 &#8211; generator &#8211; yield&#8217;s"},"content":{"rendered":"<p>php 7 has now implement <a href=\"http:\/\/php.net\/manual\/en\/language.generators.syntax.php#control-structures.yield\" target=\"_blank\" rel=\"noopener\">generator<\/a>s where you are able to return (yield) results from a method and only the ones that you will need.<\/p>\n<pre lang=\"php\">\/\/ define the output as type Generator -- e.g. yield results.. the compiler will still work without this Generator keyword\nfunction yieldSomeNumbers() : Generator {  \n    yield 10;\n    yield 13;\n}\n\nforeach (yieldSomeNumbers() as $v) {\n    var_dump($v);\n}\n<\/pre>\n<p>Will output<\/p>\n<pre lang=\"console\">10\n13\n<\/pre>\n<p>The &#8216;Generator&#8217; at the end of the method &#8221; : Generator&#8221; is not actual needed as the compiler will append it as such on the fly since the method is yield results.<\/p>\n<p>For example, lets say that you are doing a search of numbers from 1-100 and are searching for the value of 10, so before generators the code would have been something like<\/p>\n<pre lang=\"php\">function generateNumbers() {\n    return range(1,100);   \/\/ load up a array of values 1-100 e.g. 1,2,3,4,5...\n}\n\nforeach (generateNumbers() as $v){\n    var_dump($v);\n    if ($v == 10) {\n        var_dump(\"FOUND\");\n        break;\n    }\n}\n<\/pre>\n<p>The &#8216;foreach (generateNumbers()&#8217; will be using the full array where as<\/p>\n<pre lang=\"php\">function generateSomeNumbers() : Generator {\n    foreach (range(1,100) as $v) {\n        yield $v;\n    }\n}\n\nforeach (generateSomeNumbers() as $v){\n    var_dump($v);\n    if ($v == 10) {\n        var_dump(\"FOUND\");\n        break;\n    }\n}\n<\/pre>\n<p>will only return each yield upon request.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>php 7 has now implement generators where you are able to return (yield) results from a method and only the ones that you will need. \/\/ define the output as type Generator &#8212; e.g. yield results.. the compiler will still work without this Generator keyword function yieldSomeNumbers() : Generator { yield 10; yield 13; } &hellip; <a href=\"https:\/\/www.codingfriends.com\/index.php\/2017\/02\/01\/php-7-generator-yields\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">php 7 &#8211; generator &#8211; yield&#8217;s<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[413,410,414],"class_list":["post-1846","post","type-post","status-publish","format-standard","hentry","category-php","tag-generator","tag-php7","tag-yield"],"_links":{"self":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/1846","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/comments?post=1846"}],"version-history":[{"count":4,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/1846\/revisions"}],"predecessor-version":[{"id":1867,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/1846\/revisions\/1867"}],"wp:attachment":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/media?parent=1846"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/categories?post=1846"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/tags?post=1846"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}