18
$apple="";
$banana="";
$apple="Red";
$banana="Blue";

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

How can I select a random string (fast) via PHP?

2
  • 3
    BTW you dont need to declare variables before use - you can just create $apple="red"; Commented Jan 5, 2012 at 12:07
  • Thanks for that! A bad habit from bad experience - I just do it now for extra safety! Commented Jan 5, 2012 at 12:09

6 Answers 6

43

What about:

$random = rand(0, 1) ? 'Red' : 'Blue';
Sign up to request clarification or add additional context in comments.

Comments

32

Issue with your code

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().

Solutions

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);

2 Comments

Main differences between shuffle() and array_rand()
Is that a question? If so their respective code samples above and the relevant linked manual pages clearly show the difference.
13

Use an array :

$input = array("Red", "Blue", "Green");
echo $input[array_rand($input)];

3 Comments

Repped and this is also an answer to mean - clean simple two line code. Marked the other answer due to explanation and so other may benefit.
@TheBlackBenzKid im sorry I dont follow ? "Repped and this is also an answer to mean" ???
I marked this answer as UP - I "repped" it; I gave it reputation - plus points. And to mean that I am using the code for my solution - the code was not marked as answer fully as the answer above has more explanation that others may benefit - but for your efforts and post I wanted to give some "karma" as they say.
6

Ran into this old question that tops google's results. You can have more than two options and still get it all on one line.

echo ['green', 'blue', 'red'][rand(0,2)];

Comments

3

array_rand() is probably the best way:

$varNames = array('apple','banana');
$var = array_rand($varNames);
echo ${$varNames[$var]};

Comments

2

The function rand() has two parameters: a lower-limit for a random number and an upper limit. You can not use variables like that because it is not the way the function works.

Take a look at the documentation:
http://php.net/rand

A simple way to achieve what you want is this:

$array = array();
$array[0] = 'banana';
$array[1] = 'orange';
$randomSelected = $array[rand(0,(count($array)-1))];

As far as I've read, this solution is faster than array_rand(). I can see if I can find the source of that.

10 Comments

Considering this - however everyone on here is saying array_rand()
I would just use array_rand() as this is much harder to read and work out what is going on. Simplicity of maintenance trumps these kinds of micro-optimisations every time.
True, but most programmers are capable of figuring out this rather simple use of two functions.
So what exactly is the saving over just using array_rand()? CPU proc time is much cheaper than programmers reading odd implementations of standard patterns even if your method is marginally faster.
For speed. This got it. I am only interested in speed. Thanks
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.