Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8642403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T11:46:04+00:00 2026-06-12T11:46:04+00:00

I had no idea something like this (javascript-esque inline function) would work in PHP.

  • 0

I had no idea something like this (javascript-esque inline function) would work in PHP. As someone who spends a lot of time writing javascript/jquery, I was pretty stunned when my ‘i wonder if this will work’-attempt actually worked.

usort($inbox_messages, function($a, $b){
    return strtotime($b["date"]) - strtotime($a["date"]);
});

Now, is there anything I should know about implementations like that? Performance issues, backwards compatibility, scope etc.

Is this common practice, or bad code?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T11:46:05+00:00Added an answer on June 12, 2026 at 11:46 am

    This feature is called anonymous functions, and it becomes available since PHP 5.3:

    Anonymous functions, also known as closures, allow the creation of
    functions which have no specified name. They are most useful as the
    value of callback parameters, but they have many other uses.

    There’s a (slight) difference in scope resolution as well for them:

    Closures may also inherit variables from the parent scope. Any such
    variables must be declared in the function header. Inheriting
    variables from the parent scope is not the same as using global
    variables.

    So, in this example:

    $total = 0.00;
    $callback = function ($quantity, $product) use ($tax, &$total)
    {
        $pricePerItem = constant(__CLASS__ . "::PRICE_" . strtoupper($product));
        $total += ($pricePerItem * $quantity) * ($tax + 1.0);
    };
    array_walk($this->products, $callback);
    

    … from that documentation page both $tax and $total are effectively injected into a closure ($callback) scope without using global keyword.

    Anonymous functions are in fact objects of Closure class. And it’s not just an implementation feature: as (since PHP 5.4) these functions can use $this object (similar to a context object in JavaScript), there are some useful methods that can change this context (Closure::bind, Closure::bindTo – compare these with Function.bind, for example).

    Speaking more specifically, it’s actually a good practice using anonymous functions when map/filter-ing collection, as these functions won’t go into the global scope. But, of course, it’s not a solution if you need your code to work on PHP 5.2.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When I did something like this: $(#show).click(function(){ {% for p in collection %} options.push({
I had en idea, which shortly explained was that i would like to load
I had an idea that would require me be able to send and receive
Hey guys i had an idea yesterday. Can you help me with this. Here
I just got this question on an interview and had no idea how to
I'm looking for an easy way to implement something like this: http://www.dinointeractive.com/?p=117 . And
I have very strange problem with IsNumeric function in classic asp fail. Something like
I'm not sure if something like this is even possible in Python, but if
First I thought that I had to convert JavaScript to PHP, but then I
I had the idea to write a matrix division in Haskell as Muliply A*

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.