Home >Backend Development >PHP Tutorial >How to correctly use PHP random number class_PHP tutorial

How to correctly use PHP random number class_PHP tutorial

WBOY
WBOYOriginal
2016-07-15 13:33:491106browse

PHP random number code example:

  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...
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