What best practices should be followed when creating a PHP function to iterate through an array and modify its values?
When creating a PHP function to iterate through an array and modify its values, it is important to ensure that the function is clear, efficient, and follows best practices. This includes using a foreach loop to iterate through the array, passing the array by reference to modify its values directly, and using meaningful variable names to improve readability.
function modifyArrayValues(array &$arr) {
foreach ($arr as &$value) {
// Modify the value here
$value = $value * 2;
}
}
$array = [1, 2, 3, 4, 5];
modifyArrayValues($array);
print_r($array);