PHP Undefined variable in a closure
Inheriting variables from the parent scope
$row = array_map(function($n) use ($data) {
$name = sprintf('point[%0d]', $n+1);
$value = $data['measurements'][$n];
return form_input($name, $value, "class='input-mini'");
}, range($i*6, $i*6+5));
Undefined variable inside a closure in PHP
You have to use the 'use'
keyword to pass variables from the parent scope to the closure:
@for ($i=1; $i <= 12; $i++)
{!!
($substance->consumptions->filter(function($consumption, $key) use ($i){
return $consumption->date->month == $i;
})->sum('quantity'))
!!},
@endfor
Hope this helps
why undefined variable in closure laravel?
u create a closure
but did not pass parameter into Closure
so your code is not working
since inside the closure PHP doesn't know the $my_profile_id
variable you need to have a use
statement in the closure too
$my_user_id = Auth::user()->id;
$my_profile_id = DB::table('profiles')->select('profiles.profile_id', 'profiles.id')->leftjoin('users','users.id' ,'=' ,'profiles.id')->where('users.id',$my_user_id)->pluck('profiles.profile_id')->first();
$friend_accept = DB::table('friend_request')->select('profiles.profile_id','profiles.first_name','interest_request.sent_at','interest_request.interest_id')->leftjoin('profiles', function($join){
$join->on('interest_request.sender_id' , '=','profiles.profile_id');
$join->orOn('interest_request.to_user_id' , '=','profiles.profile_id');
})->where('to_user_id',$my_profile_id)
->orwhere('sender_id',$my_profile_id)
->where('interest_status','2')
->whereNotIn('profiles.profile_id', function($q) use ($my_profile_id) {
$q->select('profiles.profile_id')->from('profiles')
->where('profiles.profile_id',$my_profile_id);
})->groupby('profiles.profile_id')->get();
for more info about Closure
see
Lumen 5.3 Undefined variable: closure
The closure should be the last item in your array. Change your route to:
$app->get('/', ['middleware'=>'auth', function () use ($app) {
return $app->version();
}]);
Laravel Cache, variable inside closure undefined?
You should allow anonymous function to capture the variable outside the scope. $entity_id
its outside the scope. This is how php express the closure. With a use()
would work.
public function entity($entity_id, Request $request)
{
$expiresAt = Carbon::now()->addMinutes(10);
$entity = Cache::remember('entities', $expiresAt, function () use($entity_id) {
return Entity::where('id', $entity_id)
->with('address')
->with('_geoloc')
->first();
});
Why is a variable used by reference to the set_error_handler function closure undefined?
The &$error
is making the code work. Basically, it's assigning the $error
variable to null when you pass the callable, and then inside, it's setting it to the error message, and used afterwards.
set_error_handler(function ($type, $msg) use (&$error) { $error = $msg; });
$moved = move_uploaded_file($this->getPathname(), $target);
restore_error_handler();
Then it is using the $error
variable if it failed to move:
if (!$moved) {
throw new FileException(sprintf('Could not move the file "%s" to "%s" (%s)', $this->getPathname(), $target, strip_tags($error)));
}
It's the same as:
$error = null;
set_error_handler(function ($type, $msg) use (&$error) { $error = $msg; });
Without the &
, it will throw an undefined variable notice.
And no the $error
variable is not related to the error
property.
You can take this code for example:
function executeCallback($callable) {
$callable();
}
executeCallback(function () use (&$error) {
$error = 'This is the error message.';
});
echo $error;
The $error
variable is defined in the use (&$error)
and used afterward. And the output of the echo
would be:
This is the error message.
Related Topics
PHP String Concatenation - "$A $B" VS $A . " " . $B - Performance
Using PHP, How to Insert Text Without Overwriting to the Beginning of a Text File
Laravel Expected Response Code 250 But Got Code "530"
How to Select a Result from the Select2 Search Results
Upload Images Through PHP Using Unique File Names
What Does an Exclamation Mark Before a Function Really Mean in PHP
Access JSON Object Name in PHP
How to Use Bind_Param for Order By
PHP $_Server['Php_Self'] to Include Query String
What Is Laravel Render() Method For
PHP $String{0} VS. $String[0];
Nginx + PHP-Fpm 7.1 - 504 Gateway Time-Out
Follow Redirects with Curl in PHP
PHP Upload Image to Remote Server with Curl
Different Recipients Based on Product Category in Woocommerce Email Notification