开发者

PHP rand from list

What is the best way of doing this ?

rand('Helloworld','Hi world');

I know this is not working as 开发者_开发问答rand needs a min and max number, but what is the solution for this ?


Use array_rand.


Something likes that:

<?php
$array = array(1, 2, 3);
$array[ mt_rand( 0, count($array) -1 ) ];
?>


PHP's array_rand() for sure. Example:

<?php

$names = array('Neo', 'Morpheus', 'Trinity', 'Cypher', 'Tank');
echo 'Hello ' . $names[array_rand($names)];


function rand_of() {
    $_ = func_get_args();
    return $_[rand(0, count($_) - 1)];
}

echo rand_of("foo", "bar", "baz");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜