How can the code be optimized to display the desired output of league pairings after every 4 matches?

The code can be optimized by adding a condition to display the league pairings after every 4 matches. This can be achieved by keeping a counter variable to track the number of matches and only displaying the pairings when the counter is a multiple of 4. By doing this, the output will only show the league pairings after every 4 matches.

$matches = array("Team A vs Team B", "Team C vs Team D", "Team E vs Team F", "Team G vs Team H", "Team I vs Team J", "Team K vs Team L", "Team M vs Team N", "Team O vs Team P");

$counter = 0;
foreach ($matches as $match) {
    $counter++;
    echo $match . "<br>";
    
    if ($counter % 4 == 0) {
        echo "League pairings after 4 matches<br>";
        // Display league pairings logic here
    }
}