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

การสร้างสตริงสุ่มโดยใช้ PHP


ในการสร้างสตริงสุ่มโดยใช้ 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