php抽奖小程序的实现代码

yipeiwu_com6年前PHP代码库

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

复制代码 代码如下:

<?php
 /**
  * “抽奖”函数
  *
  * @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);
 ?>

相关文章

PHP实现对二维数组某个键排序的方法

本文实例讲述了PHP实现对二维数组某个键排序的方法。分享给大家供大家参考,具体如下: /** * 对查询结果集进行排序 * @access public * @param ar...

PHP开启opcache提升代码性能

配置指令如下: [opcache] zend_extension=opcache.so opcache.enable_cli=1 ;共享内存大小, 这个根据你们的需求可调 opcac...

利用curl 多线程 模拟 并发的详解

首先,先了解下 php中的curl多线程函数:复制代码 代码如下:# curl_multi_add_handle# curl_multi_close# curl_multi_exec#...

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 2611816 bytes)

今天要用php代码来处理一个580M的日志文件,总共有219万多行记录,因为是.log的文件,在windows下面很难将文件按照条数来分割,于是在linux下用split -l 1000...

PHP产生随机字符串函数

<?php  /**   * 产生随机字符串   *   * 产生一个指定长度的随机字符串...