Q:

How to display div only once in the loop and next div display inside div which run only once

I try to found the solution on google and SO but I haven't got solution.
I have a code something like.
$index = 0;
while (some condition here) { 
         if ($index < 4) {?>
            <div class="first4">
              <p>Some text here</p>
            </div>
    <?php }
        else{
            $check=0;
            if ($check==0){?>
              <div class="displayOnceInwhile">
            <?php $check=1; }?>
            <div class="InsideaboveClass"></div>

    <?php } 

$index++;}?>

What I am doing with the above code is, if $index is less then 4 then the inner text will display else $check will run only once in the loop but it's not working. Also, Notice here I confused where should I closed the displayOnceInwhile closing </div>.
Expected result
<!--first 4 will display-->
    <div class="first4"><p>Some text here</p></div>
     <div class="first4"><p>Some text here</p></div>
     <div class="first4"><p>Some text here</p></div>
     <div class="first4"><p>Some text here</p></div>
<!--Set will display like this-->
     <div class="displayOnceInwhile">
      <div class="InsideaboveClass"></div>
    </div>

A:

Hope This is what you are trying to do is.
<?php
$check = 0;
$index = 0;
while (some condition here) { 
    if ($index < 4) {
        echo '<div class="first4"><p>Some text here</p></div>';
    } else {
        if ($check==0){
            echo '<div class="displayOnceInwhile">';
            $check=1;
        }
        echo '<div class="InsideaboveClass"></div>';
    }
    $index++;
}
echo '</div>';
?>

