Random output from an array in PHP
Let's say you want to output a value between string0 and string7, at random, with PHP:
<?php
$randomString = array(
"string0",
"string1",
"string2",
"string3",
"string4",
"string5",
"string6",
"string7"
);
print_r($randomString [array_rand($randomString )]);
?>
One point you should be aware of is that, if you leave array_rand empty, it will return the array key, not its value. Hence the repetition of $randomString...
Written by Serge K. Keller
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Php
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#