What are the potential pitfalls of using nested loops in PHP for calculating ratios with specific constraints?

Using nested loops for calculating ratios with specific constraints can lead to inefficient code and slow performance, especially if the loops have a large number of iterations. To solve this issue, consider using a single loop with appropriate conditions to calculate the ratios based on the constraints. This approach can improve the code's efficiency and readability.

// Example of calculating ratios with specific constraints using a single loop
$total = 0;
$validCount = 0;

foreach ($data as $value) {
    // Apply constraints here
    if ($value >= $minValue && $value <= $maxValue) {
        $total += $value;
        $validCount++;
    }
}

$ratio = $validCount > 0 ? $total / $validCount : 0;

echo "Ratio with specific constraints: " . $ratio;