Maison > développement back-end > tutoriel php > PHP获取随机数经验之谈_PHP教程

PHP获取随机数经验之谈_PHP教程

WBOY
Libérer: 2016-07-15 13:33:43
original
1098 Les gens l'ont consulté

作用:

取得随机字符串

PHP获取随机数的参数:

1、(int)$length = 32 #随机字符长度
2、(int)$mode = 0 #随机字符类型,0为大小写英文和数字,1为数字,2为小写字母,3为大写字母,4为大小写字母,5为大写字母和数字,6为小写字母和数字

PHP获取随机数定义类代码

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><span> ?php   </span></span></span></li>
<li><span>class getRandstrClass   </span></li>
<li class="alt"><span>{   </span></li>
<li>
<span>function getCode ($</span><span class="attribute">length</span><span> = </span><span class="attribute-value">32</span><span>, $</span><span class="attribute">mode</span><span> = </span><span class="attribute-value">0</span><span>)   </span>
</li>
<li class="alt"><span>{   </span></li>
<li><span>switch ($mode) {   </span></li>
<li class="alt"><span>case '1':   </span></li>
<li>
<span>$</span><span class="attribute">str</span><span> = </span><span class="attribute-value">'1234567890'</span><span>;   </span>
</li>
<li class="alt"><span>break;   </span></li>
<li><span>case '2':   </span></li>
<li class="alt">
<span>$</span><span class="attribute">str</span><span> = </span><span class="attribute-value">'abcdefghijklmnopqrstuvwxyz'</span><span>;   </span>
</li>
<li><span>break;   </span></li>
<li class="alt"><span>case '3':   </span></li>
<li>
<span>$</span><span class="attribute">str</span><span> = </span><span class="attribute-value">'ABCDEFGHIJKLMNOPQRSTUVWXYZ'</span><span>;   </span>
</li>
<li class="alt"><span>break;   </span></li>
<li><span>case '4':   </span></li>
<li class="alt">
<span>$</span><span class="attribute">str</span><span> = </span><span class="attribute-value">'ABCDEFGHIJKLMNOPQRSTUVWXYZa<br>bcdefghijklmnopqrstuvwxyz'</span><span>;break;   </span>
</li>
<li><span>case '5':   </span></li>
<li class="alt">
<span>$</span><span class="attribute">str</span><span> = </span><span class="attribute-value">'ABCDEFGHIJKLMNOPQRSTUVWXY<br>Z1234567890'</span><span>;   </span>
</li>
<li><span>break;   </span></li>
<li class="alt"><span>case '6':   </span></li>
<li>
<span>$</span><span class="attribute">str</span><span> = </span><span class="attribute-value">'abcdefghijklmnopqrstuvwxyz<br>1234567890'</span><span>;   </span>
</li>
<li class="alt"><span>break;   </span></li>
<li><span>default:   </span></li>
<li class="alt">
<span>$</span><span class="attribute">str</span><span> = </span><span class="attribute-value">'ABCDEFGHIJKLMNOPQRSTUVWXYZa<br>bcdefghijklmnopqrstuvwxyz1234567890'</span><span>;   </span>
</li>
<li><span>break;   </span></li>
<li class="alt"><span>}   </span></li>
<li>
<span>$</span><span class="attribute">randString</span><span> = </span><span class="attribute-value">''</span><span>;   </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">len</span><span> = </span><span class="attribute-value">strlen</span><span>($str)-1;   </span>
</li>
<li>
<span>for($</span><span class="attribute">i</span><span> = </span><span class="attribute-value">0</span><span>;$i </span><span class="tag"><span> $length;$i ++){   </span></span>
</li>
<li class="alt">
<span>$</span><span class="attribute">num</span><span> = </span><span class="attribute-value">mt_rand</span><span>(0, $len);   </span>
</li>
<li>
<span>$randString </span><span class="attribute">.</span><span>= $str[$num];   </span>
</li>
<li class="alt"><span>}   </span></li>
<li><span>return $randString ;   </span></li>
<li class="alt"><span>}   </span></li>
<li><span>}   </span></li>
<li class="alt">
<span class="tag">?></span><span>  </span>
</li>
</ol>
Copier après la connexion

PHP获取随机数使用说明:

1.将以上代码另存为random.php
2.在需要的页面引入random.php

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><span> ?php   </span></span></span></li>
<li><span>require_once('random.php');   </span></li>
<li class="alt">
<span class="tag">?></span><span>  </span>
</li>
</ol>
Copier après la connexion

3. 使用之前定义的类

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><span> ?php   </span></span></span></li>
<li>
<span>$</span><span class="attribute">code</span><span> = </span><span class="attribute-value">new</span><span> getRandstrClass();   </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">length</span><span> = </span><span class="attribute-value">32</span><span>;   </span>
</li>
<li>
<span>$</span><span class="attribute">mode</span><span> = </span><span class="attribute-value">0</span><span>;   </span>
</li>
<li class="alt">
<span>$</span><span class="attribute">str</span><span> = $code-</span><span class="tag">></span><span>getCode($length, $mode);   </span>
</li>
<li><span>echo $str;   </span></li>
<li class="alt">
<span class="tag">?></span><span>   </span>
</li>
</ol>
Copier après la connexion

以上就是PHP获取随机数的全部应用方法。


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/446045.htmlTechArticle作用: 取得随机字符串 PHP获取随机数的参数: 1、(int)$length = 32 #随机字符长度 2、(int)$mode = 0 #随机字符类型,0为大小写英文和数字,1为数...
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal