16

In manual there is create_function function and you can pass result from that function to array_map, I thought that that is the only way to have something like anonymous functions and closures, but then I found that I can just put function like in javascript

array_map(function($a) {
    return $a + 1;
}, array(1, 2, 3, 4, 5));

In which version of php I can do this? Was this always there?

jcubic
  • 61,973
  • 54
  • 229
  • 402

3 Answers3

25

Closures (anonymous functions) were added in PHP 5.3.0, including the use clause.


Then since PHP 5.4.0 the static keyword is supported in front of it to denote a static function.

And as of PHP 7.4.0 arrow functions (RFC) as a more concise syntax.

hakre
  • 193,403
  • 52
  • 435
  • 836
Mark Baker
  • 209,507
  • 32
  • 346
  • 385
5

Anonymous functions are available since PHP 5.3:

The key features of PHP 5.3.0 include:

knittl
  • 246,190
  • 53
  • 318
  • 364
1

PHP >5.3:

http://php.net/manual/en/functions.anonymous.php

Daan
  • 3,403
  • 23
  • 19