$value=array("Rabin","Reid","Cris","KVJ","John");
$rand_keys=array_rand($value,2);
echo "First random element = ".$value[$rand_keys[0]];
echo "<br>Second
random element = ".$value[$rand_keys[1]];
We can create a six digit random number generator which will give us only characters.
srand ((double) microtime() * 10000000);
$input = array ("A", "B", "C", "D", "E","F","G","H","I","J","K","L","M","N","O","P","Q",
"R","S","T","U","V","W","X","Y","Z");
$rand_index = array_rand($input,6);
print $input[$rand_index[3]];
print $input[$rand_index[5]];
print $input[$rand_index[4]];
print $input[$rand_index[2]];
print $input[$rand_index[1]];
print $input[$rand_index[0]];
By using above code we can generate 6 digit random number of only characters. You can read how to generate random numbers here.
<?php
$names = array("Alice", "Bob", "Charlie", "David");
$random_key = array_rand($names);
echo $names[$random_key]; // Outputs a random name
?>
<?php
$names = array("Alice", "Bob", "Charlie", "David");
$random_keys = array_rand($names, 2);
echo $names[$random_keys[0]] . ", " . $names[$random_keys[1]]; // Outputs two random names
?>
<?php
$fruits = array("a" => "Apple", "b" => "Banana", "c" => "Cherry");
$random_key = array_rand($fruits);
echo "$random_key => " . $fruits[$random_key]; // Outputs a random key and value
?>
<?php
$fruits = array("a" => "Apple", "b" => "Banana", "c" => "Cherry");
$random_keys = array_rand($fruits, 2);
foreach ($random_keys as $key) {
echo "$key => " . $fruits[$key] . "<br>";
}
?>
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.
| gab | 21-02-2015 |
| Very nice script | |