php - 在循环中多次使用 $I++
问题描述
我需要在许多地方使用 $i++ 的值,但如果我这样做,那么某些字段最终会跳过值,而不是 1、2、3 等。它们是 1、3、5,而另一个字段的值为 2 , 4, 6 等
<?php $i = 1; ?>
<?php while (have_rows( 'something' ) ): the_row(); ?>
<div>This number: <?php echo $i++; ?></div>
<div>Should be the same as this one: <?php echo $i++; ?></div>
<?php end while; ?>
如果我创建另一个变量,它会起作用,但这感觉就像一个黑客。
<?php $i = 1;
$x = 1;
?>
<?php while (have_rows( 'something' ) ): the_row(); ?>
<div>This number: <?php echo $i++; ?></div>
<div>Should be the same as this one: <?php echo $x++; ?></div>
<?php end while; ?>
有没有更好的办法?
解决方案
解决方案很简单——$i
只增加一次:
<?php $i = 1; ?>
<?php while (have_rows( 'something' ) ): the_row(); ?>
<div>This number: <?php echo $i; ?></div>
<div>Should be the same as this one: <?php echo $i++; ?></div>
<?php end while; ?>
推荐阅读
- reactjs - 如何通过 React Context API 使用到达路由器?
- c# - 使用 U-Net 在场景之间传输数据
- jasperserver - 将 Jasperreports 服务器从 7.2 升级到 7.5 时出错(密钥库问题)
- java - 为什么 Firebase 身份验证使用这么多存储空间?
- passwords - 密码保护应用程序,因此只有特定用户才能创建帐户
- html - Word html 格式:通过域代码插入自定义目录
- postgresql - Sequelize upsert 为具有可为空的唯一列的架构引发唯一违规
- javascript - 异步函数作为 socketio 侦听器的处理程序
- python - 有没有办法将 signal.fftconvolve 与一组不同的内核一起使用?
- unity3d - 如何在 Android 上运行 Unity HDRP 项目