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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:48:08+00:00 2026-06-03T00:48:08+00:00

In PHP, if you want to access variable in the outer scope, you need

  • 0

In PHP, if you want to access variable in the outer scope, you need to declare it explicitly, e.g.

$foo = 'bar';
func (function() use ($foo) {

    echo $foo;

});

But in JavaScript, they are implicit, e.g.

foo = 'bar';
func (function() {
    console.log(foo);
});

What are the advantages and disadvantage of these two type of closure?

  • 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-03T00:48:10+00:00Added an answer on June 3, 2026 at 12:48 am

    In PHP, if you want to access variable in the outer scope, you need to
    declare it explicitly […] use ($foo)

    Technically, your function is not accessing $foo in the outer scope. To do that, you would need to:

    $foo = 'bar';
    $func = function() {
        global $foo;
        echo $foo;
    };
    

    This is not a closure. No variables are closed in with the function. If the value of $foo is changed, the next call to func will reflect that:

    $func(); // bar
    $foo = 'baz';
    $func(); // baz
    

    However, if we close in $foo with func:

    $foo = 'bar';
    $func = function() use ($foo) {
        echo $foo;
    };
    
    $func(); // bar
    $foo = 'baz';
    $func(); // bar
    

    func‘s $foo will retain it’s value because it’s been closed-over into the function.

    To do the same in JavaScript, you simply create a function within a function and a closure will be created (giving the inner function access to the enclosing function’s scope):

    function getFunc(foo) {
        return function () {
            console.log(foo);
        };
    }
    
    foo = "bar";
    func = getFunc(foo);
    func(); // bar
    foo = "baz";
    func(); // bar
    

    What are the advantages and disadvantage of these two type of closure?

    Using a “heap” type scope, as opposed to stack, so that the variable environment stays attached to the function allows first-class functions to be much more flexible as they can be passed around and recalled without worrying about creating (or passing in) a certain set of variables in order to make the function usable.

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

Sidebar

Related Questions

I want the value of JavaScript variable which i could access using PHP. I
I want to use an access card reader with PHP. I am doing this
I want to set a variable in index.php and want to access that variable
I want my function to access an outside variable—from its parent function specifically. However,
I want my PHP extension to declare a class equivalent to the following PHP:
I am using codeigniter framework and I want to access a multidimensional config variable
I need to find out how to access data variable outside of the post
How to access PHP session variables from jQuery function in a .js file? In
Possible Duplicate: How to access a variable in a PHP class that is set
Using this function I want to create a PHP navigation: function loadPage($pagename) { if

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.