Assign numbers to PHP phrases

Asked

Viewed 386 times

1

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

  • 1

    Those phrases are where?

  • 1

    And will appear where?

  • @rray they will be written in advance, if applicable, they will receive the value of a variable

  • @Caiofelipepereira will appear as soon as the user logs into their account.

  • 1

    Are in a? file in the bank? is one per user?

  • @rray they do not yet have a specific location, where it would be easier to use them?

  • 1

    Will they appear when the user sends the form (on the login screen, I presume), or on the next screen after logging in? I can see that your question is very comprehensive, right?!

Show 2 more comments

2 answers

5

From what you specified in your question I can give you an idea of how to do this simply, use an array to store your sentences and have the function array_rand() resume an Indice random that will be the chosen phrase.

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

echo $frases[array_rand($frases)];

See working on Ideone

4


Try this way

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

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

echo $array[$aleatorio];
  • 2

    It worked right, thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.