What is the correct way to increment a variable in PHP within a loop to sum up specific entries?

When incrementing a variable within a loop to sum up specific entries in PHP, you need to initialize the variable outside the loop and then increment it within the loop based on your conditions. This ensures that the variable retains its value across iterations and only adds up the desired entries.

// Initialize the variable outside the loop
$totalSum = 0;

// Loop through the entries and sum up specific values
foreach ($entries as $entry) {
    if ($entry meets specific condition) {
        $totalSum += $entry;
    }
}