Generate a word between words defined [PHP]

1

I'm pretty new to PHP , but I already know the basics of HTML , and a large part of Ruby script (not ruby on rails ).

Well, you know, how can I generate a word or something random, we can say, but not so random. As if it were to generate a word from a "database". For example:

I define two words in my code: Language , Language and Lang . I want to make php generate one of them, but it does not generate something like language, or anything other than Language , Language or Lang p>     

asked by anonymous 07.06.2015 / 00:16

2 answers

3

You can do this:

$palavras = array('Linguagem', 'Língua', 'Lang');
$aleatorio = rand(0, 2);
echo $palavras[$aleatorio];

Example: link

Define an array with the words you want to use, then rand() to generate a random number The method rand() allows to indicate the minimum and maximum number, notice that I used 0 and 2 because the index of arrays starts at zero.

You can simply $aleatorio = rand(0, count($palavras) - 1); and it is independent of the number of array elements.

Example using rand(0, count($palavras) - 1); and a function: link

    
07.06.2015 / 00:20
1

You can use shuffle to shuffle the array and use current to return the first element.

$array = array( 'Linguagem', 'Língua', 'Lang' );

shuffle( $array );
echo current( $array );

Example

    
07.06.2015 / 00:23