- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
PHP program different ways to generate a random string
Using bin2hex function
Example
<?php $num = 12; $res = bin2hex(random_bytes($num)); print_r("The randomly generated string is : "); echo $res; ?>
Output
The randomly generated string is : f1db16115fa93b98493d388b
A number is defined and the bin2hex function is called on this number. Inside the bin2hex function the ‘random_bytes’ function is called on this number. The generated random string is printed on the console.
Using hashing function
Example
<?php $my_str = rand(); $res = sha1($my_str); print_r("The randomly generated string using hashing function sha1 is :"); echo $res; ?>
Output
The randomly generated string using hashing function sha1 is :9a4a73c35ac034832332977f3d5accd8eace5260
A number is defined by calling the ‘rand’ function. The sha1 hashing function is called on this randomly generated number. The generated random string is printed on the console.
Using built-in function uniqid
Example
<?php $res = uniqid(); print_r("The randomly generated string using uniqid function is : "); echo $res; ?>
Output
The randomly generated string using uniqid function is : 5ed4b884cef34
A number is defined by calling the ‘rand’ function. The sha1 hashing function is called on this randomly generated number. The generated random string is printed on the console.
- Related Articles
- Java Program to generate random numbers string
- Generate a random string in Java
- Java program to generate random numbers
- C++ program to generate random alphabets
- C++ program to generate random number
- How to generate different random numbers in a loop in C++?
- Generate random string/characters in JavaScript?
- C# program to generate secure random numbers
- C# Program to generate random lowercase letter
- C++ Program to Generate Random Hexadecimal Bytes
- Generate random number from 0 – 9 in PHP?
- Generate array of random unique numbers in PHP?
- Python Generate random string of given length
- C++ Program to Generate a Random Subset by Coin Flipping
- Java Program to generate a random number from an array

Advertisements