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读取文本文件并逐行输出该行使用最多的字符与对应次数的方法。分享给大家供大家参考,具体如下: test.txt文件: Welcome to our website...

php中this关键字用法分析

本文实例讲述了php中this关键字用法。分享给大家供大家参考,具体如下: 下面定义了一个Cart类 <?php class Cart { var $items;...

ajax php 实现写入数据库

首先需要一个带输入表格. 复制代码 代码如下:<!-- To change this template, choose Tools | Templates and open the...

php使用substr()和strpos()联合查找字符串中某一特定字符的方法

本文实例讲述了php使用substr()和strpos()联合查找字符串中某一特定字符的方法。分享给大家供大家参考。具体分析如下: 要求:在下面的字符串中查找双竖线前面的字符,大多数清空...

自制PHP框架之设计模式

为什么要使用设计模式? 设计模式,我的理解是为了达到“可复用”这个目标,而设计的一套相互协作的类。 感兴趣的读者可以阅读《Design Patterns: Elements of Reu...