Home  >  Article  >  php教程  >  php抽奖小程序的实现代码

php抽奖小程序的实现代码

WBOY
WBOYOriginal
2016-06-13 11:47:121037browse

这个抽奖小程序,在实际的测试环境中也可以用到,比方说测试数据的查询在in条件下,要查询随机的5个id,然后在用ab去压测

复制代码 代码如下:


 /**
  * “抽奖”函数
  *
  * @param integer $first    起始编号
  * @param integer $last     结束编号
  * @param integer $total    获奖人数
  *
  * @return string
  *
 */
 function isWinner($first, $last, $total)
 {
     $winner = array();
     for ($i=0;;$i++)
     {
         $number = mt_rand($first, $last);
         if (!in_array($number, $winner))
             $winner[] = $number;    // 如果数组中没有该数,将其加入到数组
         if (count($winner) == $total)   break;
     }
     return implode(' ', $winner);
 }
 // for test
 echo isWinner(1, 100, 5);
 ?>


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn