Web Development

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:

[gads]

User defined function

This function may be defined using syntax such as following:
[cc lang=”php”]
[/cc]

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.

[cc lang=”php”]

[/cc]

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:
[cc lang=”php”]

[/cc]

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.

[cc lang=”php”]

[/cc]

[gads]

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:

[cc lang=”php”]

[/cc]

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

Shares:
  • 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.

    Reply
  • Tiara Brafman
    September 25, 2010 at 5:55 am

    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.

    Reply
  • Sherlene Sturman
    Sherlene Sturman
    September 26, 2010 at 3:41 am

    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!

    Reply
  • Baldo Geevarghese
    September 26, 2010 at 5:29 pm

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

    Reply
  • Kerry Laprise
    Kerry Laprise
    October 3, 2010 at 12:38 pm

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

    Reply
  • Neelam Panthi
    Neelam Panthi
    March 31, 2013 at 1:24 pm

    This is very helpful site for any topic

    Reply
  • shetu
    shetu
    December 20, 2019 at 2:57 pm

    Great Site for learner.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *