Different function types in PHP

In this post I will explain the different types of function which comes into play while working with PHP.

Below are the List of PHP function types:

  • User defined function
  • Returning value function
  • Variable function
  • Internal (in-built) function
  • Anonymous function

Now let’s start each type in detail:

User defined function

This function may be defined using syntax such as following:

<?php
  function bar($arg1, $arg2,,)
  {
    echo “test function;
  }
?>

Function names follow the same rules as other labels in PHP. A valid function name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: [a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*.

You can see the Function naming rules at here.

Function needs to be defined before they are referenced, except when function is conditionally defined.

When a function is defined in conditional manner such as below example, It must be processed prior to being called.

<?php
  $makefoo = true;
  /* We can't call foo() from here
  since it doesn't exist yet,
  but we can call bar() */

 
  bar();
  if ($makefoo)
  {
    function foo()
    {
      echo "I don't exist until program execution reaches me.\n";
    }
  }

/* Now we can safely call foo()
since $makefoo evaluated to true */


  if ($makefoo) foo();
  function bar()
  {
    echo "I exist immediately upon program start.\n";
  }
?>

Returning value function

This is same as user defined function but it will return some values based on passed argument using return statement. Check below example:

<?php
  function square($len)
  {
    return 4*$len;
  }
  square(4);
?>

Variable Function

PHP support the concept of the variable function. This means that if a variable name has parentheses appended to it, PHP will look for a function with the same name as the whatever variable evaluate to, and will attempt to execute it. This can be used to implement callbacks, function tables, etc.

<?php
  function bar()
  {
    echo "This is the output fo the bar function";
  }
  $foo='bar';
  $foo();  // This will call the bar function
?>

Internal ( in-built) Function

PHP comes with some standard functions and constructors. There also some functions which require to enable PHP extension, otherwise fata “undefined function” error will appear.

For example : to use mysql_connect(), PHP must be compiled with MySQL support.

Anonymous Function

Anonymous functions, also known as closures, allow creation of functions which have no specified name.

Closures can also be used as the values of variables; PHP automatically converts such expressions into instances of the Closure internal class. Assigning a closure to a variable uses the same syntax as any other assignment, including the trailing semicolon:

<?php
  $unnamedFunction = function($name){
    printf("Hello %s\r\n", $name);
  };
 
  $unnamedFunction('World');
  $unnamedFunction('PHP');
?>

Hope you find this information useful for you. Waiting for your responses.

Related Posts

Written by Avinash

Avinash Zala is leading various projects which deals with the various technology involved with the web. A combination of perfect technical and management skills. Avinash would like to chat with you and convert your imagination into the working system. You can get in touch with him on Facebook and Twitter.

View all posts by: