Symfony 7 – PHP – Anonymous Functions and Closures

20 multiple-choice questions about anonymous functions, closures, variable scope, binding, and practical examples in PHP.


Question 1: What is an anonymous function in PHP?

Question 2: Which syntax defines an anonymous function?

Question 3: What keyword allows an anonymous function to use variables from outside its scope?

Question 4: What will this code print? $y=10; $f=function($x) use($y){return $x+$y;}; echo $f(5);

Question 5: How can a closure modify an external variable?

Question 6: What happens if you modify $y after defining the closure? $y=5; $f=function() use($y){return $y;}; $y=10; echo $f();

Question 7: What does Closure::bind() allow you to do?

Question 8: What will this output? $x=3; $y=function() use($x){return $x*2;}; echo $y();

Question 9: What is returned by function test(){return function($x){return $x+1;};}?

Question 10: What will echo here? $a=function($n){return $n*$n;}; echo $a(4);

Question 11: What does Closure::fromCallable([$obj, "method"]) do?

Question 12: What’s the main difference between anonymous functions and arrow functions (fn)?

Question 13: What will this print? $x=5; $f=fn($y)=>$x+$y; echo $f(3);

Question 14: Which type does every anonymous function return when created?

Question 15: What is the purpose of Closure::call($object)?

Question 16: What is printed? $a=1;$f=function()use(&$a){$a++;};$f();echo $a;

Question 17: What is true about closures inside loops?

Question 18: What does the keyword static mean inside an anonymous function?

Question 19: What’s the output? function get(){return function(){return "Hello";};} $a=get(); echo $a();

Question 20: Why are closures useful for callbacks and array functions (like array_map)?