What best practices should be followed when setting up conditions to find the largest value in an array in PHP?

When setting up conditions to find the largest value in an array in PHP, it's important to initialize a variable to hold the current maximum value and then iterate through the array to compare each element with the current maximum. If an element is greater than the current maximum, update the maximum value. Finally, return the maximum value after iterating through the entire array.

function findLargestValue($arr) {
    $max = $arr[0];
    
    foreach ($arr as $value) {
        if ($value > $max) {
            $max = $value;
        }
    }
    
    return $max;
}

// Example usage
$array = [3, 7, 2, 10, 5];
echo findLargestValue($array); // Output: 10