Different function types in PHP

  • Avinash
  • 6
  • Sep 23, 2010
  • Web Development

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:

  • http://www.articlesbase.com/news-and-society-articles/scam-verizon-reverse-lookup-1750847.html reverse phone lookup name

    I can testify that your headline “different function types in php xpert developer…” is terrific but I want to let you know that your content is now working with my Sony Ericsson Xperia browser.

  • http://www.great-tutorials.info Tiara Brafman

    Great advice I recently hit on your blog and have been reading along. I thought I would leave my incipient comment. I don’t know what toexposeexcept that I have enjoyed reading. On target blog. I will keep visiting this blog very without exception.

  • Sherlene Sturman

    Appreciate it for give rather decent information. Your world-wide-web is great.I am impressed by the specifics that you’ve on this blog. It exhibits how well you comprehend this subject. Bookmarked this method page, will come back again for extra. You, my buddy, awesome! I found just the information I previously looked for in all places and just wasn’t able to discover. What a ideal internet site. Such as this web site your website is one of my new favorite.I similar to this information proven and it has given me some sort of encouragement to have good results for some reason, so maintain up the great function!

  • http://www.autopano.net/forum/profile.php?section=personality&id=18611 Baldo Geevarghese

    Super-Duper site! I am enjoying it!! Will come back once again – taking you feeds also, cheers.

  • Kerry Laprise

    You’re getting better and better as a writer. Keep it up!

  • Neelam Panthi

    This is very helpful site for any topic