Complete the code to create a closure that uses the variable $x.
<?php $x = 5; $func = function() use ([1]) { return $x * 2; }; echo $func(); ?>
use keyword.The use keyword imports the variable $x into the closure's scope.
Complete the code to modify the external variable $count inside the closure.
<?php $count = 0; $increment = function() use (&[1]) { $count++; }; $increment(); echo $count; ?>
& to pass by reference.use.Using &$count imports the variable by reference, allowing the closure to modify it.
Fix the error in the closure by correctly importing the variable $name.
<?php $name = "Alice"; $greet = function() use ([1]) { return "Hello, " . $name; }; echo $greet(); ?>
The closure must import $name using use ($name) to access it inside.
Fill both blanks to create a closure that captures $a and $b by value and returns their sum.
<?php $a = 3; $b = 4; $sum = function() use ([1], [2]) { return [1] + [2]; }; echo $sum(); ?>
use.The closure imports $a and $b by value and returns their sum.
Fill all three blanks to create a closure that captures $x by reference, increments it, and returns the new value.
<?php $x = 10; $increment = function() use ([1]) { [2]++; return [3]; }; echo $increment(); ?>
use.The closure imports $x by reference using &$x, increments it, and returns the updated value.