Anonymous Functions

aka Closures or Lambdas.


Requires PHP 5.3


Allows functions with no name.


An unnamed function (closure) is assigned to a variable as follows:


$myVar = function($myArg) {

echo "$myARG";

} ;


*notice the variable assignment still closes with a semi-colon ;


So far we have assigned an anonymous function to $myVar, which expects an argument $myArg to be passed to it


We can now use that variable as though it were a function:


	$myFunction = function($myArg) {
		echo "$myArg";
	} ; //notice the closing semi-colon

	myFunction("Hello, World!");


	$website = function($myArg) {
		return ("https://www." . $myArg . "<br>") ;

	echo $website("tech-academy") ;
	echo $website("google") ;

Leave a Reply