Home>Article>Backend Development> Detailed usage methods in PHP closure function() use()
php's closure (Closure) is alsoanonymous function. It was introduced in PHP5.3.
The syntax of closure is very simple. The only keyword that needs attention is use. Use means to connect the closure and external variables.
Several functions of closure:
1 Reduce the code of foreach loop
1 2 3 |
##} |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
##[php] view plain
// A basic shopping cart, including some added products and each quantity of goods. // One method is used to calculate the total price of all items in the shopping cart. This method uses a closure as a callback function. class {
return #public $tax
1.0 ); #$this $total }
|
If we transform the getTotal function here, we must use foreach
2 to reduce the parameters of thefunction
1 2 3 4 5 6 7 8 9 |
##$class
"$open$inner$close" |
3 Unlockrecursive functions
2345678910 | [php] view plaincopy
Note that use in the above question uses &. If you do not use & here, an error will occur. n-1) The function cannot be found (the type of fib was not defined previously) So I want to use closure to release it. When looping functions, you need to use
For more PHP related technical articles, please visit the PHP Tutorialcolumn to learn! |
The above is the detailed content of Detailed usage methods in PHP closure function() use(). For more information, please follow other related articles on the PHP Chinese website!