PHP从两个字符串中选择一个随机字符串

前端之家收集整理的这篇文章主要介绍了PHP从两个字符串中选择一个随机字符串前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
$apple="";
$banana="";
$apple="Red";
$banana="Blue";

$random(rand($apple,$banana);
echo $random;

如何通过PHP选择随机字符串(快速)?

您的代码问题

PHP rand()函数将两个数字作为输入,以形成从中选择随机数的范围.你不能提供它的字符串.

PHP manual page for rand().

解决方

你可以使用array_rand()

$strings = array(
    'Red','Blue',);
$key = array_rand($strings);
echo $strings[$key];

另一种选择是使用shuffle().

$strings = array(
    'Red',);
shuffle($strings);
echo reset($strings);
原文链接:https://www.f2er.com/php/137294.html

猜你在找的PHP相关文章