首页 > 后端开发 > php教程 > PHP获取随机数经验之谈_PHP教程

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

WBOY
发布: 2016-07-15 13:33:43
原创
1083 人浏览过

作用:

取得随机字符串

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>
登录后复制

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>
登录后复制

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>
登录后复制

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


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/446045.htmlTechArticle作用: 取得随机字符串 PHP获取随机数的参数: 1、(int)$length = 32 #随机字符长度 2、(int)$mode = 0 #随机字符类型,0为大小写英文和数字,1为数...
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板