Two conditions in a for loop

Why does this for a loop work with each condition independently, but together the first condition does not matter?

for ($j = 0; $j < 5 or $j < $synCount; $j++)

I want the loop to run five times

or

if the synCount value is less than this.

+5
source share
6 answers

You probably mean " $junder 5 and $j under $sysCount" or:

$j < min(5, $sysCount)
+12
source

very simple

$j < min(5, $sysCount)
+2
source

break for:

for ($j = 0; $j < 5 ; $j++)
{
   if( $j >= $synCount )
      break;
   //treatment
}

:

$ max = $synCount < 5 ? $synCount : 5;
for ($j = 0; $j < $synCount  ; $j++)
{
   //treatment
}

, : min():

for ($j = 0; $j < min(5, $synCount)  ; $j++)
{
   //treatment
}
0

:

$loopcount = ($syncount < 5) ? $syncount : 5;
for ($j = 0; $j < $loopcount; ++$j) {
}

, $syncount 5 , $loopcount . .

0
for ($j = 0; $j < ($syncCount <= 5 ? $syncCount : 5); $j++)

( , 5 )

for ($j = 0, $limit = min($syncCount, 5); $j < $limit; $j++)

Anothe nice solutions

foreach (range(0, min($syncCount, 5)) as $j)

Sidenote

$syncCount <= 5 ? $syncCount : 5  ==  min($syncCount, 5)
0
$productsprice=ProductPrice::model()->findAllByAttributes(array ('product_id'=>$products_data->product_id));
foreach($productsprice AS $productsprice):
   for($quantity = 0; $quantity <= 10; $quantity++)
   {
      echo '<li >'.array(value=>'ProductPrice::model()->getquantity($data->quantity)').'</li>' ;
   }
 endforeach;
0

All Articles