PHP随机字符串

PHP随机字符串,php,Php,我想在我的页面中打印随机值。 我编写了此代码,但它不起作用: $claim[1] = "Red"; $claim[2] = "Blue"; $claim[3] = "Yellow"; $claim[4] = "Purple"; $claim[5] = "Magenta"; $color = $claim[mt_rand(1,2,3,4,5)];

我想在我的页面中打印随机值。 我编写了此代码,但它不起作用:

    $claim[1] = "Red";
    $claim[2] = "Blue";
    $claim[3] = "Yellow";
    $claim[4] = "Purple";
    $claim[5] = "Magenta";
    $color = $claim[mt_rand(1,2,3,4,5)];    

有什么想法吗?

您需要一个介于1到5之间的随机数:

$claim[1] = "Red";
$claim[2] = "Blue";
$claim[3] = "Yellow";
$claim[4] = "Purple";
$claim[5] = "Magenta";
$color = $claim[rand(1,5)]; 
可选:但也可以使用array_rand(返回随机键数组):


它怎么不起作用?你有错误吗?如果是,是什么?你预计会发生什么?到底发生了什么?你做了什么来调试这个?约翰,我希望我写的那页打印上面的一种颜色。但是它不打印任何东西。你从哪里得到的
mt_rand(1,2,3,4,5)
?手册清楚地解释了可以调用此函数的参数类型,不是吗?当你去发明你自己的伪造的无意义语法时,你不必怀疑你的东西不起作用。太好了!谢谢您!
$claim[1] = "Red";
$claim[2] = "Blue";
$claim[3] = "Yellow";
$claim[4] = "Purple";
$claim[5] = "Magenta";
$color = $claim[array_rand($claim)];