Breaking out of a loop

A loop can be defined as

<br data-mce-bogus="1">

for($i = 1; $i&lt;10;$i++){

// Do something

}

Let us consider that application logic dictates that when we $i ==5 , then we exit the loop


for($i = 1; $i<10;$i++){

// Do something

if($i == 5)

break;

}

We can apply the same thing in while


while($i<10)

{

if($i == 5)

break;

$i++;

}

It is better to put the break condition as

if(5==$i) because many a developers have been ruined by this

if ($i=5) ( missing “=” )

Leave a Reply

Your email address will not be published. Required fields are marked *