From the course: Learning PHP

Unlock the full course today

Join today to access over 24,700 courses taught by industry experts.

Anonymous functions

Anonymous functions - PHP Tutorial

From the course: Learning PHP

Anonymous functions

- [Instructor] Another way to write functions is anonymously. Anonymous functions don't get a name and are often created at runtime to perform a specific task. This is slightly more efficient than defining a function if you're only going to use it once. These are also called closures or callback functions. The latter because sometimes functions in PHP require a different function as the second argument. PHP is basically saying, we will do some of the work, but you need to tell us how to do it. One common example of this is the usort function in PHP. You could see that it accepts an array as its first value and it is being passed by reference and then, the second argument is a callable compare function. So usort will sort the array, but you need to tell it how you want to compare the items. Since we're only going to use this function for usort, it doesn't make sense for us to define a separate function and keep it in PHPs…

Contents