What are the best practices for implementing a group break in PHP when displaying grouped data from a database query?

When displaying grouped data from a database query in PHP, it is best practice to use a group break to separate the different groups of data for better readability and organization. This can be achieved by checking if the current group value is different from the previous one, and if so, output a break to visually separate the groups.

// Assuming $results is an array of grouped data from a database query
$prevGroup = null;

foreach ($results as $row) {
    if ($row['group'] != $prevGroup) {
        echo '<h2>' . $row['group'] . '</h2>'; // Output group title
        $prevGroup = $row['group']; // Update previous group value
    }

    echo '<p>' . $row['data'] . '</p>'; // Output data within the group
}