note 50344 deleted from function.rand by felipe
| From: | felipe@php.net | Date: | Sun, 30 Dec 2007 18:42:28 +0000 |
| Subject: | note 50344 deleted from function.rand by felipe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-134409@lists.php.net to get a copy of this message | ||
Note Submitter: horroradore at gmail dot com
----
Here's the simplest way I've found to get random something without the need for an
external file. Simple, but effective.
<?php
function func(){
//Randomize selection. In this case, 5 (0,1,2,3,4,5).
$x = rand()&4;
$vars = array(
//Insert your data into array.
0 => "var1",
1 => "var2",
2 => "var3",
3 => "var4",
4 => "var5"
);
echo $vars[$x];
}
?>
The only problem with that is that it's not really random, and that you sometimes get the same
result several times. I've seen sollutions to this using microtime(), but I can't think of
them off the top of my head.