Add one to number each time it appears
Posted: Wed Sep 12, 2007 3:06 pm
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!
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
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;
Ha ha. I'm really not experienced with functions() Is there a tutorial or something that could show me how to do the carraige return?feyd wrote:Not having a carriage return after the number echo out.
Code: Select all
for ($x=0;$x<10;$x++) {
echo counter() . '<br />';
}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?