Starting with the version 5.3, PHP introduced Anonymous functions, known as Closures.
This type of functions have no specified name. They are most useful as the value of callback.
- Anonymous function as callback function. Example:
- Closures can also be used as the values of variables (notice how the function is called, using the variable name, including the $ character). Example:
- Closures can also be used inside another function. In the following example it is used closures to create HTML List elements.
- Another example with anonymous function inside other function. Notice the using of the use() , to can use the outside variables inside anonymous function.
- Closures can be a useful tool for object-oriented programming. Starting with PHP 5.4 , $this can be used in anonymous functions.