How to Generate random a String In PHP
Hi Guys,
In this example,I will leran you how to generate random string in php.you can simply and easy to generate random string in php.
For Example,random function is first argument how to get character for string and second argument length to string.bellow example
Example :
<?php
function RandomStringGenerator($length)
{
// Variable which store final string
$randomString = "";
// small letters, capital letters and digits
$characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
// Find the length of created string
$len = strlen($characters);
// Loop to create random string
for ($i = 0; $i < $length; $i++)
{
// Generate a random index to pick
$index = rand(0, $len - 1);
// Concatenating the character
$randomString = $randomString . $characters[$index];
}
return $randomString;
}
$length = 10;
echo "Random String of length " . $length
. " = " . RandomStringGenerator($length);
?>
It will help you.....
- How To Use Php Conditional Statement Example
- How to Check if String Contains Regex in PHP?
- PHP Array_column() Function Example
- PHP Generate List of Random Numbers Between 0 and 100 Example
- How to User Login with Facebook in PHP?
- How to Explode Every Character into Array Using PHP Example?
- PHP Form Validation Tutorial
- PHP Convert XML to JSON Example