Tools, FAQ, Tutorials:
Randomly Retrieving Array Values in PHP
How To Randomly Retrieve a Value from an Array in PHP?
✍: FYIcenter.com
If you have a list of favorite greeting messages, and want to randomly select one of them to be used in an email, you can use the array_rand() function. Here is a PHP example script:
<?php
$array = array("Hello!", "Hi!", "Allo!", "Hallo!", "Coucou!");
$key = array_rand($array);
print("Random greeting: ".$array[$key]."\n");
?>
This script will print:
Random greeting: Coucou!
⇒ Looping through an Array without "foreach" in PHP
⇐ Using an Array as a Stack in PHP
2017-01-11, ∼3090🔥, 0💬
Popular Posts:
How to read Atom validation errors at w3.org? If your Atom feed has errors, the Atom validator at w3...
How To Pass Arrays By References? in PHP? Like normal variables, you can pass an array by reference ...
How To Change Text Fonts for Some Parts of a Paragraph? If you want to change text fonts or colors f...
How to dump (or encode, serialize) a Python object into a JSON string using json.dumps()? The json.d...
How to access Query String parameters from "context.Request.Url.Que ry"object in Azure API Policy? Q...