$apple="";
$banana="";
$apple="Red";
$banana="Blue";
$random(rand($apple, $banana);
echo $random;
How can I select a random string (fast) via PHP?
What about:
$random = rand(0, 1) ? 'Red' : 'Blue';
The PHP rand()
function takes two numbers as input to form the range to pick a random number from. You cannot feed it strings.
See the PHP manual page for rand()
.
You can use array_rand()
:
$strings = array(
'Red',
'Blue',
);
$key = array_rand($strings);
echo $strings[$key];
Another option is to use shuffle()
.
$strings = array(
'Red',
'Blue',
);
shuffle($strings);
echo reset($strings);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With