>  기사  >  php教程  >  PHP 연구 노트: 임의의 이름을 얻으려면 time 및 mt_rand 함수를 사용하세요.

PHP 연구 노트: 임의의 이름을 얻으려면 time 및 mt_rand 함수를 사용하세요.

WBOY
WBOY원래의
2016-09-20 03:30:221179검색

이 지식은 파일 업로드 및 기타 상황에서 사용됩니다. 아직 객체 지향을 배우지 않았으므로 코드를 주석으로 표시했습니다. 이해하지 못하시면 온라인으로 질문하실 수 있습니다.

<span style="font-size: 18px;"><?<span style="color: #000000;">php
</span><span style="color: #008000;">/*</span><span style="color: #008000;">*
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/9/19 0019
 * Time: 19:47
 </span><span style="color: #008000;">*/</span>
<span style="color: #0000ff;">function</span><span style="color: #000000;"> rdname(){
    </span><span style="color: #800080;">$t</span> = <span style="color: #008080;">getdate</span><span style="color: #000000;">();
    </span><span style="color: #800080;">$year</span>=<span style="color: #800080;">$t</span>['year'<span style="color: #000000;">];
    </span><span style="color: #800080;">$month</span> = <span style="color: #800080;">$t</span>['mon']<10? "0".<span style="color: #800080;">$t</span>['mon']:<span style="color: #800080;">$t</span>['mon'<span style="color: #000000;">];
    </span><span style="color: #800080;">$day</span> = <span style="color: #800080;">$t</span>['mday']>9?<span style="color: #800080;">$t</span>['mday']:"0".<span style="color: #800080;">$t</span>['mday'<span style="color: #000000;">];
    </span><span style="color: #800080;">$hour</span>= <span style="color: #800080;">$t</span>['hours']>9?<span style="color: #800080;">$t</span>['hours']:"0".<span style="color: #800080;">$t</span>['hours'<span style="color: #000000;">];
    </span><span style="color: #800080;">$min</span> = <span style="color: #800080;">$t</span>['minutes']>9?<span style="color: #800080;">$t</span>['minutes']:"0".<span style="color: #800080;">$t</span>['minutes'<span style="color: #000000;">];
    </span><span style="color: #800080;">$sec</span> = <span style="color: #800080;">$t</span>['seconds']>9?<span style="color: #800080;">$t</span>['seconds']:"0".<span style="color: #800080;">$t</span>['seconds'<span style="color: #000000;">];
    </span><span style="color: #800080;">$randnum</span>=<span style="color: #008080;">mt_rand</span>(1000,9999);<span style="color: #008000;">//</span><span style="color: #008000;">获取随机的4个数字</span>
    <span style="color: #800080;">$randname</span>=<span style="color: #800080;">$year</span>.<span style="color: #800080;">$month</span>.<span style="color: #800080;">$day</span>.<span style="color: #800080;">$hour</span>.<span style="color: #800080;">$min</span>.<span style="color: #800080;">$sec</span>.<span style="color: #800080;">$randnum</span>;<span style="color: #008000;">//</span><span style="color: #008000;">把年月日时分和随机数拼接起来</span>
   <span style="color: #0000ff;">return</span> <span style="color: #800080;">$randname</span>;<span style="color: #008000;">//</span><span style="color: #008000;">返回新的名字</span>
<span style="color: #000000;">}
</span><span style="color: #800080;">$newname</span>=<span style="color: #000000;">rdname();
</span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$newname</span><span style="color: #000000;">;
</span>?><?<span style="color: #000000;">php
</span><span style="color: #008000;">/*</span><span style="color: #008000;">*
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/9/19 0019
 * Time: 19:47
 </span><span style="color: #008000;">*/</span>
<span style="color: #0000ff;">function</span><span style="color: #000000;"> rdname(){
    </span><span style="color: #800080;">$t</span> = <span style="color: #008080;">getdate</span><span style="color: #000000;">();
    </span><span style="color: #800080;">$year</span>=<span style="color: #800080;">$t</span>['year'<span style="color: #000000;">];
    </span><span style="color: #800080;">$month</span> = <span style="color: #800080;">$t</span>['mon']<10? "0".<span style="color: #800080;">$t</span>['mon']:<span style="color: #800080;">$t</span>['mon'<span style="color: #000000;">];
    </span><span style="color: #800080;">$day</span> = <span style="color: #800080;">$t</span>['mday']>9?<span style="color: #800080;">$t</span>['mday']:"0".<span style="color: #800080;">$t</span>['mday'<span style="color: #000000;">];
    </span><span style="color: #800080;">$hour</span>= <span style="color: #800080;">$t</span>['hours']>9?<span style="color: #800080;">$t</span>['hours']:"0".<span style="color: #800080;">$t</span>['hours'<span style="color: #000000;">];
    </span><span style="color: #800080;">$min</span> = <span style="color: #800080;">$t</span>['minutes']>9?<span style="color: #800080;">$t</span>['minutes']:"0".<span style="color: #800080;">$t</span>['minutes'<span style="color: #000000;">];
    </span><span style="color: #800080;">$sec</span> = <span style="color: #800080;">$t</span>['seconds']>9?<span style="color: #800080;">$t</span>['seconds']:"0".<span style="color: #800080;">$t</span>['seconds'<span style="color: #000000;">];
    </span><span style="color: #800080;">$randnum</span>=<span style="color: #008080;">mt_rand</span>(1000,9999);<span style="color: #008000;">//</span><span style="color: #008000;">获取随机的4个数字</span>
    <span style="color: #800080;">$randname</span>=<span style="color: #800080;">$year</span>.<span style="color: #800080;">$month</span>.<span style="color: #800080;">$day</span>.<span style="color: #800080;">$hour</span>.<span style="color: #800080;">$min</span>.<span style="color: #800080;">$sec</span>.<span style="color: #800080;">$randnum</span>;<span style="color: #008000;">//</span><span style="color: #008000;">把年月日时分和随机数拼接起来</span>
   <span style="color: #0000ff;">return</span> <span style="color: #800080;">$randname</span>;<span style="color: #008000;">//</span><span style="color: #008000;">返回新的名字</span>
<span style="color: #000000;">}
</span><span style="color: #800080;">$newname</span>=<span style="color: #000000;">rdname();//把返回的名字赋值给</span>$newname</span>
<span style="font-size: 18px;"><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$newname</span><span style="color: #000000;">; </span>?></span>

효과 표시:

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.