php抽奖小程序的实现代码_php技巧

WBOY
Release: 2016-05-17 09:00:39
Original
1997 people have browsed it

这个抽奖小程序,在实际的测试环境中也可以用到,比方说测试数据的查询在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);
 ?>

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template