试图找出为什么这个PHP while循环实际上工作

This question is based on a reply I got from a question I submitted earlier:

Print new <div> on id iteration change inside php while loop from MySQL result

It's not really a problem, but I'm trying to understand what's going on.

I can't figure out why this loop does not add </div> to the variable $html at the very first iteration when $previous is equal to null

$previous = null;

while ($result = $stmt->fetch()) {

    if ($_company_id !== $previous) {
        if($previous !== null) {
            $html .='</div>';
        }
        $html .= '<div id="company-' . $_company_id . '" class="tab-pane fade">';
    }

    $previous = $_company_id;

}

if($previous != null) {
    $html.='</div>';
}

The output is exactly as I want it:

<div id="company-1" class="tab-pane fade"></div>
<div id="company-2" class="tab-pane fade"></div>

But I just can't understand why. Can anyone please explain this to me?

Actually because they check IF IS NOT. Take a closer look at

 if($previous !== null) {
   $html .='</div>';
 }

So it gets skipped on the first go through. On Every other Turn it has a value and the condition is true. hence executing the code.