Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> PHP

การสร้าง Random String ด้วย PHP


เพื่อสร้าง random string ด้วย PHP โค้ดมีดังนี้ −

ตัวอย่าง

<?php 
    $res = substr(md5(mt_rand()), 0,5); 
    echo "Displaying random string...\n"; 
    echo $res; 
?>

ผลลัพธ์

จะผลลัพธ์ดังนี้ −

Displaying random string... 1c856

ตัวอย่าง

มาดูตัวอย่างอีกอย่าง −

<?php 
    $res = substr(md5(mt_rand()), 0,5); 
    echo "Displaying random string...\n"; 
    echo $res; 
    echo "\nDisplaying another random string...\n"; 
    $res2 = bin2hex(random_bytes(5)); 
    echo $res2; 
?>

ผลลัพธ์

จะผลลัพธ์ดังนี้ −

Displaying random string... a3541 Displaying another random string... 335b83d9e9