1

I need to access the first argument to a function inside an anonymous function passed as the second argument to said function. For example:

<?php
function a($arg, $func) {
    echo $func();
}
a("argument 1", function () use($arg) {return $arg;});
?>

The above will return the following error:

Notice: Undefined variable: arg in path/to/file.php on line 5

While the desired result would be:

argument 1

1 Answer 1

2

Try below solution:

function a($arg, $func) {
    echo $func($arg);
}
a("argument 1", function ($arg) {return $arg;});

output

argument 1

for more detail have a look at http://php.net/manual/en/functions.anonymous.php

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.