Simple way to change the value of a variable within a closer function

Snippets 1 min read

If you want to change a value of a variable within a closer function, use the & before each variable.


$roles = [];

$users->each(function ($user) use ($roles) {
	$roles = $user->roles;
});

Output

  // [] empty array

Use the & before the variable.

$users->each(function ($user) use ($roles) {
	$roles = $user->roles;
});

Output

 // array of rules  

Related Tags

About the Author

Oussama's Profile Picture
Oussama
Full Stack Web Developer

I'm a full stack web developer and telecommunications engineer who love to share knowledge and build stuff online!


Comments

Join Our Newsletter

Subscribe to Our Newsletter and never miss our offers, latest news, Articles, etc.

Our news letter sent once a week, every tuesday.