Assign numbers to phrases in PHP

1

Hello, I have a certain number of phrases, and I need every time the user sends a certain form, one of these phrases appears, using a random function, but I do not know how to start, I have no code so far.

    
asked by anonymous 27.10.2015 / 12:05

2 answers

4

Try this way

$array = array(
    'Frase 1',
    'Frase 2',
    'Frase 3'
);

$aleatorio = rand(0, count($array) -1);

echo $array[$aleatorio];
    
27.10.2015 / 12:17
5

From what you specified in your question I can give you an idea how to do this in a simple way,    use an array to store your sentences and have the array_rand () function return an index    randomly chosen phrase.

$frases = array('frase 1','frase 2','frase 3','frase 4');

echo $frases[array_rand($frases)];

See working at Ideone

    
27.10.2015 / 12:17