How can PHP developers efficiently handle arrays as parameters in callback functions?
When passing arrays as parameters in callback functions in PHP, developers can efficiently handle them by using the `use` language construct to pass the array by reference. This allows the callback function to access and modify the array directly without making a copy of it.
// Define an array
$array = [1, 2, 3, 4, 5];
// Callback function that modifies the array
$callback = function() use (&$array) {
$array[] = 6;
};
// Call the callback function
$callback();
// Output the modified array
print_r($array);
Related questions
-
PHP role-based access control conditional statement
-
nl2br line breaks preg_replace
-
isset cookies PHP