note 50344 added to function.rand
| From: | horroradore at gmail dot com | Date: | Fri, 25 Feb 2005 04:03:32 +0000 |
| Subject: | note 50344 added to function.rand | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-85524@lists.php.net to get a copy of this message | ||
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.
----
Manual Page -- http://www.php.net/manual/en/function.rand.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+50344
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+50344&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+50344&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+50344&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+50344&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+50344&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+50344&report=yes
Search -- http://master.php.net/manage/user-notes.php