How to understand the PHP 5.3 closure syntax
This article will explain in detail how to understand the PHP 5.3 closure syntax, the content of the article is of high quality, so the editor shares it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.
PHP 5.3 will add closure syntax, that is, anonymous functions, which allow developers to declare inline functions and save them in variables. Although this syntax is a bit odd compared to JavaScript's closures, it is a good complement to the PHP language.
For example, you can use it like this now:
$closure = function ($param) {echo $param;}; / / This one takes value of someVar and "stores" it in the closure's scope even if / / we later change the value of someVar outside it. / / We assume that $somerVar is defined before this $closure2 = function ($param) use ($someVar) {echo $param. ''. $someVar;}
For example, closures are useful in the output HTML:
Function item_list (array $items, $formatter = null) {/ / create the default formatter if ($formatter = = null) {$formatter = function ($row) {return'< p >'. $row. '< / p >';} $html ='< h3 > Listing: < / h3 >'; foreach ($items as $item) {$html. = $formatter ($item);} return $html;} on how to understand the syntax of PHP 5.3 closures is shared here. I hope the above can be helpful and learn more. If you think the article is good, you can share it for more people to see.