how to add odd/even loop in array

1. ount the loop number


for($i=0;$i<$blah;$i++)
if($i&1){
// ODD
}else{
// EVEN
}



2. odd loop itteration


$oddLoop = false;
foreach ($findposts as $findpost):
//.....
if($oddLoop=!$oddLoop){
// code for odd loop numbers
}else{
// code for even loop numbers
}


3. Odd ID number

if ( ( $findpost->ID ) != $id ) {
if($findpost->ID & 1){
// ODD
}else{
//EVEN
}



The three ways are


1. Modulo
for ($i = 0; $i < 10; $i++)
{
 
if ($i % 2 == 0)
 
{
    echo
"even";
 
}
 
else
 
{
    echo
"odd";
 
}
}


2. Flipping boolean value

$even = true;
for ($i = 0; $i < 10; $i++)
{
 
if ($even)
 
{
    echo
"even";
 
}
 
else
 
{
    echo
"odd";
 
}

  $even
= !$even;
}


3. mentioned boolean operator

for ($i = 0; $i < 10; $i++)
{
 
if ($i & 1 == 0)
 
{
    echo
"even";
 
}
 
else
 
{
    echo
"odd";
 
}
}

Comments

Popular posts from this blog

HTML5 video loop src change on end play function

Set limit wp_nav_menu

Calculate percentage of a specific element has been scrolled into visible on the screen