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 7967887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T06:59:28+00:00 2026-06-04T06:59:28+00:00

This has got me very confused and I can’t seem to find an answer

  • 0

This has got me very confused and I can’t seem to find an answer to this question. A clear and simple clarification would be nice.

  • 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-04T06:59:29+00:00Added an answer on June 4, 2026 at 6:59 am

    A closure is a function that is evaluated in its own environment, which has one or more bound variables that can be accessed when the function is called. They come from the functional programming world, where there are a number of concepts in play. Closures are like lambda functions, but smarter in the sense that they have the ability to interact with variables from the outside environment of where the closure is defined.

    The use() keyword let’s you import variables from outside the function environment, inside the function. Variables to be imported from the outside environment are specified in the use clause of the closure function definition. By default, they are passed by value. So let’s say the function has no parameters, but you wan’t it to use a variable you already have.

    $string = "Hello World!";
    $closure = function() use ($string) { echo $string; };
    

    This is useful when you need to create a function what must be used as callback somewhere else, and can only have defined parameters. The use() keyword let’s you use other variables in addition to the ones you pass as function arguements. For example on the php.net example: http://php.net/manual/en/functions.anonymous.php

    public function getTotal($tax)
        {
            $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);
            return round($total, 2);
        }
    

    $callback must only have two parameters, because array_walk will only allow that much:

    Typically, funcname takes on two parameters. The array parameter’s
    value being the first, and the key/index second.

    So what can we do? We call use() to add other variables that are not the $callback’s scope, but in scope of the environment it is being called in.

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

Sidebar

Related Questions

This has to be a very simple fix, but I can't seem to figure
The answer to this has got to be simple, but I sure don't see
Have got myself very confused on this one - apologies if the answer is
I'm sure this has got a simple answer, but it's stumping me :( I'm
This has to be simple. Still, I've noticed VS2012 acting very erratic - sometimes
I want to achieve the functionality that this editor has got with its preview
I've been working on some MySQL Joins but this one has got me a
My Java application has got a package structure similar to this: src/com/name/app src/com/name/app/do src/com/name/utils/db
Given that django-nonrel has got JOINs working, does this mean we have M2M fields
Has anyone got this working in a web application? No matter what I do

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.