Anonymous functions explained
As you probably know, you define a regular function in PHP like this:
function myFunctionName() {
// (Your function code goes here)
}
When you define a function, you give it a name (
myFunctionName
in the above example). PHP then lets your code refer to this function using its name. For example, you can call your function like this:
myFunctionName();
Anonymous functions are similar to regular functions, in that they contain a block of code that is run when they are called. They can also accept arguments, and return values.
The key difference — as their name implies — is that anonymous functions have no name. Here’s a code example that creates a simple anonymous function:
// Declare a basic anonymous function // (not much use on its own!) function( $name, $timeOfDay ) { return ( "Good $timeOfDay, $name!" ); };
No comments:
Post a Comment