0

Is there a way to get the $value seen below so that it can be used in the var_dump?

// can't alter this function..
function test($callback) {
    $callback('test');
}

// can alter this in any way so long as the above function still works..
test(function ($value) {
    return $value; // how to get $value for the dump below?
});

var_dump($value); // expecting "test"

1 Answer 1

1

You'd have to use a separate variable and pass it as context to the closure via reference, using use and &:

function test($callback)
{
  $callback('test');
}

test(function ($value) use (&$result) {
  $result = $value;
});

echo $result; // 'test'

Demo: https://3v4l.org/OlBHU

Sign up to request clarification or add additional context in comments.

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.