Jump to content

What's the best way to randomly select a single variable from two variables?


Recommended Posts

On 4/15/2019 at 8:24 AM, chhorn said:

or just 

array_rand([$orange, $apple]);


array_rand only returns the key (or keys) of the randomly selected items. So, that line would only return a 0 or 1 - which is not helpful without the original array to get the value from. It makes more sense to create an array as a variable first, the use array rand to get the value like so:

$values = ['orange', 'apple'];
$randomValue = $values[array_rand($values)];
Link to comment
Share on other sites


This topic is now archived and is closed to further replies.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.