Add one to number each time it appears
Moderator: General Moderators
Add one to number each time it appears
I want to make a numeric variable (starting at 0) have 1 added to it each time it appears.
How can I do this?
THANKS!
How can I do this?
THANKS!
Rather than using a variable, use a function.
Code: Select all
function counter($counter=0) {
static $counter;
return $counter++;
}
for ($x=0;$x<10;$x++) {
echo counter();
}
echo counter();
echo counter()+5;
echo counter()+5;Awesome. Thanks. What's causing this gobbledegook to come out though? :onion2k wrote:Rather than using a variable, use a function.
Code: Select all
function counter($counter=0) { static $counter; return $counter++; } for ($x=0;$x<10;$x++) { echo counter(); } echo counter(); echo counter()+5; echo counter()+5;
123456789101617
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
I believe the suggestion is ...
It just inserts a line break after each number.
Or are you asking something else?
Code: Select all
for ($x=0;$x<10;$x++) {
echo counter() . '<br />';
}Or are you asking something else?
Ah I'm starting to understand this. Thank you - you just helped me solve my problem.Stryks wrote:I believe the suggestion is ...
It just inserts a line break after each number.Code: Select all
for ($x=0;$x<10;$x++) { echo counter() . '<br />'; }
Or are you asking something else?