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
Higher-order function
(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==== {{further information|PHP}} <syntaxhighlight lang="php"> <?php declare(strict_types=1); function twice(callable $f): Closure { return function (int $x) use ($f): int { return $f($f($x)); }; } function plusThree(int $i): int { return $i + 3; } $g = twice('plusThree'); echo $g(7), "\n"; // 13 </syntaxhighlight> or with all functions in variables: <syntaxhighlight lang="php"> <?php declare(strict_types=1); $twice = fn(callable $f): Closure => fn(int $x): int => $f($f($x)); $plusThree = fn(int $i): int => $i + 3; $g = $twice($plusThree); echo $g(7), "\n"; // 13 </syntaxhighlight> Note that arrow functions implicitly capture any variables that come from the parent scope,<ref>{{Cite web|title=PHP: Arrow Functions - Manual|url=https://www.php.net/manual/en/functions.arrow.php|access-date=2021-03-01|website=www.php.net}}</ref> whereas anonymous functions require the {{code|use}} keyword to do the same.
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)