What is the concept of a callback function in PHP and how can it be used effectively?

A callback function in PHP is a function that can be passed as an argument to another function and called within that function. This allows for greater flexibility and customization in how functions are executed. Callback functions are commonly used in event handling, asynchronous programming, and sorting algorithms.

// Example of using a callback function in PHP

function processNumbers($numbers, $callback) {
    $result = [];
    foreach ($numbers as $number) {
        $result[] = $callback($number);
    }
    return $result;
}

function squareNumber($number) {
    return $number * $number;
}

$numbers = [1, 2, 3, 4, 5];
$squaredNumbers = processNumbers($numbers, 'squareNumber');

print_r($squaredNumbers);