S17514
S17514

Reputation: 285

PHP while loop add by 2

Currently I have a code that looks like:

for ($i=0; $i<=($num_newlines - 1); $i++) 
{
$tweetcpitems->post('statuses/update', 
                    array('status' => wordFilter("The item $parts[$i]  has been released on Club Penguin. View it here:   http://clubpenguincheatsnow.com/tools/swfviewer/items.swf?id=$parts[$id]")));
sleep(90);
}

What I want to do make the "i++" part add by two and not one, but how do I do this? Please help!

Upvotes: 11

Views: 24778

Answers (3)

Fellipe Sanches
Fellipe Sanches

Reputation: 8135

For those who are looking to increment pair of numbers (like 1-2 to 3-4):

Solution one:

//initial values
$n_left = 1;
$n_right = 2;

for ($i = 1; $i <= 5; $i++) {
    
    print "\n" . $n_left . "-" . $n_right;   
    
    $n_left =+ $n_left+2;
    $n_right =+ $n_right+2; 
}
//result: 1-2 3-4 5-6 7-8 9-10

Solution two:

for ($y = 0; $y <= 9; $y+=2) {
    
    print "\n" . ($y+1) . "-" . ($y+2);  

}
//result: 1-2 3-4 5-6 7-8 9-10

Upvotes: 0

just eric
just eric

Reputation: 927

$i++ : increment by one

$i+=2 : increment by two

$i+=3 : increment by three

etc..

Upvotes: 6

John Conde
John Conde

Reputation: 219844

for ($i=0; $i<=($num_newlines - 1); $i+=2) {

Upvotes: 37

Related Questions