PHP 在 foreach 内以间隔运行,但增加了随机性


PHP to run at intervals within foreach but with added randomness

所以我有一个foreach,我想每隔6次或11次循环运行插入一点html。

所以我在每 13 条记录上的插入

if ($i % 13 == 0) { 
}

麻烦的是我想给它添加更多的随机性。

完全忘记了这叫什么

这样的事情会使一些代码在每个循环中执行$percentage的时间。例如,您可以设置$percent = 20然后代码每次迭代将仅执行 20% 的时间。

$value = rand( 0, 100 ); // Set your ranges (min/max)
$percent = 0; // Set percentage
if ( $percent >= $value )
{
    // Will only execute $percentage of the time each loop
}