繁体   English   中英

实际上在PHP中一定时间运行函数

[英]Actually run a function a certain percentage of the time in PHP

如何在PHP中编写函数以X%的时间调用给定函数(匿名)?

例如:

function call_weighted(0.1, function() {
      echo 'this is actually run approximately 10% of the times it is called';
});
function call_weighted($weight, $function) {
    if (mt_rand() < $weight * mt_getrandmax()) {
        $function();
    }
}

暂无
暂无

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

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