简体   繁体   English

如何用PHP增加变量?

[英]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). 该行将从我的数组中回显一个数字,但是我需要将该数字增加一(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. 我尝试过几种方法,例如添加+1++1 ,但不确定是否将其添加到错误的位置,或者是否应该使用完全不同的增量。

What needs to be done to increment that variable by one each time? 每次使该变量加1时需要做什么?

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

只需尝试:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM