Rumah  >  Artikel  >  php教程  >  PHP获取随机数经验之谈

PHP获取随机数经验之谈

WBOY
WBOYasal
2016-06-13 11:10:411360semak imbas

作用:

取得随机字符串

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获取随机数的全部应用方法。


Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn