0

so I have these functions

function a(){
    int c = 1;
    b(function(){echo $c;});
}

function b($code){
    $code();
}

but somehow $c becomes undefined in the anonymous function I know it's bacause that the anonymous function is it's own scope, but is there someway to make this work?

1
  • 1
    Is this PHP? Because you've tagged it as PHP, but your code isn't Commented Mar 7, 2017 at 8:21

2 Answers 2

1

Yes: you can use "use" statement.

function a()
{
    $c = 1;
    b(function() use ($c) {
        echo $c;
    });
}

function b($code){
    $code();
}
Sign up to request clarification or add additional context in comments.

Comments

0

http://php.net/manual/en/language.variables.scope.php

When you put $c inside a function it's considered to be a local scope variable.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.