欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

如何正确运用PHP随机数类_PHP教程

程序员文章站 2022-06-16 18:23:03
...

PHP随机数类代码示例:

  1. ?php
  2. class getRandstrClass{
  3. function getCode ($length = 32, $mode = 0) {
  4. switch ($mode) {
  5. case '1':
  6. $str = '1234567890';
  7. break;
  8. case '2':
  9. $str = 'abcdefghijklmnopqrstuvwxyz';
  10. break;
  11. case '3':
  12. $str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  13. break;
  14. case '4':
  15. $str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZa
    bcdefghijklmnopqrstuvwxyz'
    ;break;
  16. case '5':
  17. $str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
  18. break;
  19. case '6':
  20. $str = 'abcdefghijklmnopqrstuvwxyz1234567890';
  21. break;
  22. default:
  23. $str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcde
    fghijklmnopqrstuvwxyz1234567890'
    ;
  24. break;
  25. }
  26. $randString = '';
  27. $len = strlen($str)-1;
  28. for($i = 0;$i $length;$i ++){
  29. $num = mt_rand(0, $len);
  30. $randString .= $str[$num];
  31. }
  32. return $randString ;
  33. }
  34. }
  35. /* 使用方法
  36. $code = new getRandstrClass();
  37. $length = 4;
  38. $mode = 0;
  39. $str = $code->getCode($length, $mode);
  40. echo $str;
  41. $code = NULL;
  42. */
  43. ?>

以上这段代码就是PHP随机数类的具体使用方法。


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/446042.htmlTechArticlePHP随机数类代码示例: ?php classgetRandstrClass{ functiongetCode($ length = 32 ,$ mode = 0 ){ switch($mode){ case'1': $ str = '1234567890' ; break; case'2': $ str = 'abcde...