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

  • Home
  • SEARCH
  • 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 8033559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:44:15+00:00 2026-06-05T01:44:15+00:00

For example, if I do this: function bar(&$var) { $foo = function() use ($var)

  • 0

For example, if I do this:

function bar(&$var)
{
    $foo = function() use ($var)
    {
        $var++;
    };
    $foo();
}

$my_var = 0;
bar($my_var);

Will $my_var be modified? If not, how do I get this to work without adding a parameter to $foo?

  • 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-05T01:44:17+00:00Added an answer on June 5, 2026 at 1:44 am

    No, they are not passed by reference – the use follows a similar notation like the function’s parameters.

    As written you achieve that by defining the use as pass-by-reference:

        $foo = function() use (&$var)
    

    It’s also possible to create recursion this way:

    $func = NULL;
    $func = function () use (&$func) {
        $func();
    }
    

    NOTE: The following old excerpt of the answer (Jun 2012) was written for PHP < 7.0. As since 7.0 (Dec 2015) the semantics of debug_zval_dump() changed (different zval handling) the refcount(?) output of it differs nowadays and are not that much saying any longer (integers don’t have a refcount any longer).

    Validation via the output by not displaying $my_var changed (from 0) still works though (behaviour).

    You can validate that on your own with the help of the debug_zval_dump function (Demo):

    function bar(&$var)
    {
        $foo = function() use ($var)
        {
            debug_zval_dump($var);
            $var++;
        };
        $foo();
    };
        
    $my_var = 0;
    bar($my_var);
    echo $my_var;
    

    Output:

    long(0) refcount(3)
    0
    

    A full-through-all-scopes-working reference would have a refcount of 1.

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

Sidebar

Related Questions

Example: /** * This function will determine whether or not one string starts with
I created this example The remove function doesn't work, and I can't figure out
I have seen this example on the web: $('#questionTextArea').each( function() { var $this =
I have a html string like this: <html><body><p>foo <a href='http://www.example.com'>bar</a> baz</p></body></html> I wish to
With an example class such as this: class Test{ public function &__get($name){ print_r($name); }
It is valid JavaScript to write something like this: function example(x) { Here is
I want to ask about this function in matlab dicomread example : a =
For example : function masterMethod(this, action){ // (action); <-- But action requires $(this) to
Take the following example plugin: (function($) { $.fn.alertOnClick = function(text) { return this.each(function(){ $(this).click(alert(text));
For example, in this simple function where fun1 takes as input two numbers, adds

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.