How can functions be properly defined and used when filtering arrays in PHP?

When filtering arrays in PHP, functions can be properly defined and used by creating a custom callback function that defines the filtering criteria. This callback function should return true for elements that should be included in the filtered array, and false for elements that should be excluded. This function can then be passed as an argument to array_filter().

// Define the custom callback function
function filterFunction($value) {
    // Define the filtering criteria, for example, only include values greater than 5
    return $value > 5;
}

// Original array
$array = [2, 6, 8, 3, 10];

// Filter the array using the custom callback function
$filteredArray = array_filter($array, 'filterFunction');

// Output the filtered array
print_r($filteredArray);