How can the use of functions in PHP improve code readability and efficiency in loops?

Using functions in PHP can improve code readability and efficiency in loops by encapsulating repetitive tasks into reusable blocks of code. This can make the code easier to understand and maintain. Functions can also help in reducing code duplication and promoting the DRY (Don't Repeat Yourself) principle, leading to more efficient and concise code.

// Example of using a function in a loop to improve readability and efficiency

// Define a function to calculate the square of a number
function calculateSquare($num) {
    return $num * $num;
}

// Loop through an array and calculate the square of each number
$numbers = [1, 2, 3, 4, 5];
foreach ($numbers as $number) {
    $square = calculateSquare($number);
    echo "The square of $number is $square\n";
}