PHP Anonymous Functions

Like many programming languages, PHP contains built-in functionality to give variety to how functions are defined and called. PHP programmers can define small, anonymous functions in code to make it more readable and efficient, as they can in other programming languages such as Python. To understand how anonymous functions work in PHP, it is important to understand how PHP handles functions, and how anonymous functions work in general.

  1. Functions in PHP

    • Traditionally, a function -- in any programming language -- simply denotes a block of code which executes certain tasks. The programmer defines this block of code with a name, and then she can use that code throughout the program by calling the name of that function. The following example illustrates a PHP function with one argument . An argument serves as the input with which a function works , if it needs this.

      function timesTwo($x)

      {

      echo $x * 2;

      }

    Functions and Variables in PHP

    • Because of the nature of the PHP interpreter, programmers can use variables to call functions. A PHP programmer accomplishes this by creating a variable containing a string -- a word -- that represents the name of an existing function. The programmer can append that variable with open and closed parentheses, along with any arguments required by the function. The PHP interpreter will then call the function name matching the variable string. The following example illustrates how this works in practice:

      function func($arg){

      echo "$arg";

      }

      $f = "func";

      f("Hi!"); //calls "func()" and prints "Hi!"

    Anonymous Functions

    • Generally, an anonymous function is a function without a name. These functions exist "in-line" with the existing code. This means the program does not have a defined name or reference for the function, as it is declared on the spot. An example of an anonymous function in practice is the "lambda" function from Python. The following example shows how a Python programmer creates a function without naming it:

      >>>(lambda x: x*2)(3) //the "lambda" function only exists on this line, and has no name

      6

    PHP and Anonymous Functions

    • The PHP programming language does not have the "lambda" keyword built into its syntax, but it does allow programmers to create anonymous functions. Programmers may want to use them when the function in question is not complex or important enough to spend the time or space defining it. PHP programmers can assign a function reference to a variable. That function can be "anonymous," rather than defined beforehand. This is similar to calling a function from a variable, but without having to name the function. The following example illustrates how an anonymous function is assigned to a variable, and called:

      $f = function($arg){

      echo "$arg";

      }

      f("Hi"); //prints "Hi"

Related Searches:

References

Comments

Related Ads

Featured