What are the best practices for handling the first element of an array differently in a PHP foreach loop?

When working with a PHP foreach loop, you may need to handle the first element of an array differently than the rest. One way to achieve this is by using a flag variable to track the first iteration and execute different logic for the first element. By checking if the flag is set, you can apply specific actions only to the first element while treating the subsequent elements uniformly.

$flag = true;
foreach ($array as $key => $value) {
    if ($flag) {
        // Handle the first element differently
        $flag = false;
    } else {
        // Handle the rest of the elements
    }
}