Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Generator (computer programming)
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===PHP=== [[File:UML generator in PHP.svg|thumb|UML class diagram depiction of the inheritance chain of the Generator class in PHP]] The community of [[PHP]] implemented generators in PHP 5.5. Details can be found in the original [https://wiki.php.net/rfc/generators Request for Comments: Generators]. Infinite Fibonacci sequence: <syntaxhighlight lang="php"> function fibonacci(): Generator { $last = 0; $current = 1; yield 1; while (true) { $current = $last + $current; $last = $current - $last; yield $current; } } foreach (fibonacci() as $number) { echo $number, "\n"; } </syntaxhighlight> Fibonacci sequence with limit: <syntaxhighlight lang="php"> function fibonacci(int $limit): Generator { yield $a = $b = $i = 1; while (++$i < $limit) { yield $a = ($b = $a + $b) - $a; } } foreach (fibonacci(10) as $number) { echo "$number\n"; } </syntaxhighlight> Any function which contains a <span style="font-family: 'Courier New';">yield</span> statement is automatically a generator function.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)