Add one to number each time it appears

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
elinews
Forum Commoner
Posts: 38
Joined: Tue Aug 14, 2007 7:18 pm

Add one to number each time it appears

Post by elinews »

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!
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

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;
elinews
Forum Commoner
Posts: 38
Joined: Tue Aug 14, 2007 7:18 pm

Post by elinews »

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;
Awesome. Thanks. What's causing this gobbledegook to come out though? :

123456789101617
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Not having a carriage return after the number echo out.
elinews
Forum Commoner
Posts: 38
Joined: Tue Aug 14, 2007 7:18 pm

Post by elinews »

feyd wrote:Not having a carriage return after the number echo out.
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?

Thanks
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it has less to do with functions and more to do with simple parameters to echo(). Try the PHP_EOL constant for example.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Or '<br />' to make it more obvious in the browser.
elinews
Forum Commoner
Posts: 38
Joined: Tue Aug 14, 2007 7:18 pm

Post by elinews »

I'm really lost. If it's no trouble could someone show me how this is done?
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Post by Stryks »

I believe the suggestion is ...

Code: Select all

for ($x=0;$x<10;$x++) {
  echo counter() . '<br />';
}
It just inserts a line break after each number.

Or are you asking something else?
elinews
Forum Commoner
Posts: 38
Joined: Tue Aug 14, 2007 7:18 pm

Post by elinews »

Stryks wrote:I believe the suggestion is ...

Code: Select all

for ($x=0;$x<10;$x++) {
  echo counter() . '<br />';
}
It just inserts a line break after each number.

Or are you asking something else?
Ah I'm starting to understand this. Thank you - you just helped me solve my problem.
Post Reply