user3918845
user3918845

Reputation: 35

How to increment variable with PHP?

Here's my code:

foreach($a as $item):
      echo '<span>'.$item['idx'].'.</span>';
      echo '<a href='.$item['url'].'>'.$item['title'].'</a><br />';
      echo '<span>'.$item['description'].'</span><br /><br />';
endforeach;

This is the line I'm specifically having problems with:

echo '<span>'.$item['idx'].'.</span>';

That line will echo a number from my array, but I need to increment that number by one (1).

I've tried doing this several ways such as adding +1 or ++1 and I'm not sure if I'm adding it in the wrong place or if I should be using something totally different increment.

What needs to be done to increment that variable by one each time?

Upvotes: 0

Views: 73

Answers (2)

Marc B
Marc B

Reputation: 360572

echo '<span>', $item['idx'] + 1, '</span>';

Upvotes: 2

hsz
hsz

Reputation: 152206

Just try with:

  echo '<span>' . ($item['idx'] + 1) . '.</span>';

Upvotes: 1

Related Questions