Home > Article > Backend Development > C++, PHP, Javascript,..., support for lambda expressions
lambda
lambda expression, also called Closure (closure), also called anonymous function. Due to its power, it is supported by almost all mainstream development languages. This article attempts to list sample codes for lambda expressions in most languages and will be continuously updated in the future.
PHP’s support for lambdas
<code><span><?php</span><span>$i</span> = <span>12</span>; <span>$j</span> = <span>33</span>; <span>$callable</span> = <span><span>function</span><span>()</span><span>use</span><span>(<span>$i</span>, &<span>$j</span>)</span> {</span><span>echo</span><span>$i</span> . <span>"\n"</span>; <span>echo</span><span>$j</span> . <span>"\n"</span>; }; <span>$callable</span>(); <span>$i</span>++; <span>$j</span>++; <span>$callable</span>(); </code>
C++’s support for lambda
<code><span>#include <iostream></span><span>using</span><span>namespace</span><span>std</span>; <span>int</span> main(<span>int</span> argc, <span>char</span>** argv) { <span>int</span> i = <span>12</span>; <span>int</span> j = <span>33</span>; <span>auto</span> callable = [i, &j](){ <span>cout</span> << i << endl; <span>cout</span> << j << endl; }; callable(); i++; j++; callable(); } </code>
[=][&]
to reference all external variables. Javascript
<code><script> var <span>i</span> = <span>12</span>; var <span>j</span> = <span>33</span>; var callable = <span><span>function</span><span>()</span>{</span> alert(<span>i</span>); alert(<span>j</span>); } callable(); <span>i</span>++; <span>j</span>++; callable(); </script</code>
The above has introduced the support for lambda expressions in C++, PHP, Javascript, ..., including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.