Looping in PHP

November 9th 2010

Ok, really easy example of a while loop counter.

The script requires 2 variables:

1) What number to start on?

2) What number to end on?

Take a look...

PHP Code:
<?php
$count_up_to
=50;
$count_from=0;

// fix the offset
$count_up_to $count_up_to 1;
$i $count_from;

while (
$i <= $count_up_to) {
    
// increase the current value
    
$i $i 1;

    echo 
"Line : ".$i;
    echo 
"<br />";
}
?>

This gives you (not surprisingly) :

Text Snippet:
Line : 1
Line : 2
Line : 3
Line : 4...

--- I won't list them all. You get the idea.

...Line : 47
Line : 48
Line : 49
Line : 50

This is all fine and dandy, but this is where it all comes together.

Imagine what can be done in that loop! You could automate a number of tasks to run, or maybe put a send mail script in between them. ** don't actually try the mail() thing. Spammers are bad!!

Back in the day when email was relatively un-checked still, I once flooded a friends inbox with 500 emails.

He wasn't impressed :(