Home >Backend Development >PHP Tutorial >PHP generates random strings (pure numbers and pure letters can be specified)

PHP generates random strings (pure numbers and pure letters can be specified)

WBOY
WBOYOriginal
2016-07-25 09:13:161080browse

Example, PHP generates a random string, you can specify whether it is pure numbers, pure letters, or a mixture, and you can specify the length.

  1. //Generate a random string

  2. function rand_zifu($what,$number){
  3. $string='';
  4. for($i = 1; $i <= $number ; $i++){
  5. //Mix letters and numbers
  6. $panduan=1;
  7. if($what == 3){
  8. if(rand(1,2)==1){
  9. $what=1;
  10. } else{
  11. $what=2;
  12. }
  13. $panduan=2;
  14. }

  15. //Only numbers

  16. if($what==1){
  17. $string.=rand(0 ,9);
  18. }elseif($what==2){
  19. //Only letters
  20. $rand=rand(0,24);
  21. $b='a';
  22. for($a =0;$a < ;=$rand;$a++){
  23. $b++;
  24. } bbs.it-home.org
  25. $string.=$b;
  26. }
  27. if($panduan==2)$what=3;
  28. }
  29. return $string;
  30. }
  31. echo rand_zifu(3,10);

Copy code


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