Linked Questions
37 questions linked to/from Callback function using variables calculated outside of it
53 votes
6 answers
37k views
Pass extra parameters to usort callback [duplicate]
I have the following functions. WordPress functions, but this is really a PHP question. They sort my $term objects according to the artist_lastname property in each object's metadata. I want to pass a ...
64 votes
1 answer
35k views
PHP error. Why is "variable undefined" inside array_map? [duplicate]
I am using array_map function in my php application. I defined the array_map function like this. $ratingID = $this->db->insert_id(); $rated_item_array = array_map(function ($a) { ...
39 votes
2 answers
14k views
Access variables from parent scope in anonymous PHP function [duplicate]
I want to write a function that does some dirty work logging a transaction, but the anonymous function scope does not seem to register the parent scope $db and $value variables. How can I pass the ...
9 votes
2 answers
9k views
How to access variable from scope of parent function? [duplicate]
I want my function to access an outside variable—from its parent function specifically. However, using the global keyword sets too broad a scope; I need to limit it. How do I get this code to spit out ...
10 votes
1 answer
6k views
How can I include a variable inside a callback function? [duplicate]
I'm trying to get counts of array values greater than n. I'm using array_reduce() like so: $arr = range(1,10); echo array_reduce($arr, function ($a, $b) { return ($b > 5) ? ++$a : $a; }); This ...
12 votes
1 answer
4k views
php array_map callback parameter scope [duplicate]
In the following code the callback function passed to wrap_map can't see the argument in the outer function, why? (see code comment for detail) public static function wrap_implode($ar, $wrap, $delim){...
7 votes
4 answers
1k views
How to access an argument of the "parent" function? [duplicate]
For example I have the following code: function a($param) { function b() { echo $param; } b(); } a("Hello World!"); That throws an E_NOTICE error because $param is of course undefined (in ...
2 votes
4 answers
3k views
PHP variable defined outside callback function is unaccessible inside the function [duplicate]
I'm trying to use $variable inside my callback function. I pass it to another function like this: functionName("egTraders_ItemDataBound"), inside that function I assign it to a variable and the call ...
4 votes
3 answers
1k views
Warning: call_user_func() [function.call-user-func]: First argument is expected to be a valid callback [duplicate]
class myClass { $myVariable = 'myCallback'; function myFunction() { $body = false; $callback = $this->myVariable; function test($handle, $line) { ...
2 votes
1 answer
1k views
PHP error with variable in callback function [duplicate]
I have this function in php (laravel): public static function isUserParticipatesInTournament($tourId, $userId) { var_dump($userId); //dumped $user = User::find($userId); ...
-1 votes
1 answer
2k views
Access variables from within the callback function of the 'each' function [duplicate]
I have a Laravel ->each() function inside a loop. I want to be able to get at the loop variables from within the each function. foreach ($stringsArray as $string) { if (!empty($string)...
4 votes
1 answer
2k views
Defining variables in functions.php and access them inside functions hook in Wordpress [duplicate]
I am trying to understand Wordpress structure in more details. As advised to me, using global keyword is not a good idea. Here is what I am trying to accomplish: Inside functions.php $affID = '...
2 votes
1 answer
823 views
How to access outside variable in this kind laravel validation callback? [duplicate]
In the below function, $input['num_opt'] cannot access $input $input = $request->all(); $validator->after(function($validator) { $num_opt = $input['num_opt']; ...
3 votes
3 answers
305 views
How to access variable of enclosing function? [duplicate]
I'm using a callback function inside an other function and I need to access a variable from this enclosing function but don't know how to do that. Here is an example: function outer($flag) { $...
1 vote
2 answers
1k views
What's the alternative to using global variables in a callback function? [duplicate]
I have used PHP for a very long time, but not really used callbacks very much until quite recently. In the following code, the callback (the example is QueryPath, in case you're wondering, but it ...