Home > Backend Development > PHP Tutorial > Detailed explanation of PHP 5.3 closure syntax_PHP tutorial

Detailed explanation of PHP 5.3 closure syntax_PHP tutorial

WBOY
Release: 2016-07-15 13:28:34
Original
853 people have browsed it

PHP 5.3 closure syntax is an anonymous function, which can bring many different experiences to developers. This article introduces several ways to use it. It is slightly different from JavaScript closures. However, compared to , for example, you can now use PHP 5.3 closure syntax like this

  1. $ closure = function($param) { echo $param; };
  2.  
  3. //This one takes value of someVar and "stores" it in the closure's scope even if 
  4. / /we later change the value of someVar outside it. We assume that $somerVar is defined before this
  5. $closure2 = function($param) use ($someVar) { echo $param . ' ' . $someVar; };

For example, PHP 5.3 closure syntax is useful for closures in output HTML:

  1. functionitem_list(array $items, $formatter = null) {
  2. //create the default formatter
  3. if($formatter == null) {
  4. function($row) { >return
  5. '

    ' . $row . > }; } html = '

    Listing:

    '
  6. ;
  7. foreach
  8. ($items as
  9. $item
  10. ) { $html .= $formatter($item); } return
  11. $html; The above is the use of two different PHP 5.3 closure syntax introduced in this article. I hope it will be helpful to everyone. http://www.bkjia.com/PHPjc/446435.html
  12. www.bkjia.com
  13. true
  14. http: //www.bkjia.com/PHPjc/446435.html
  15. TechArticle
  16. PHP 5.3 closure syntax is an anonymous function, which can bring many different experiences to developers. This article introduces several ways to use it. It is slightly different from JavaScript's closure...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template