Saturday, 15 September 2012

How do I make it randomly display one item from the array in PHP? -



How do I make it randomly display one item from the array in PHP? -

i trying create when 1 of words $hello typed in randomly chooses 1 of words $bye , displays it. @ moment when seek it, says 0, 1 or 2. how go fixing randomly give me 1 of words $bye array instead.

<?php $words = $_post['words']; $hello = array('hello', 'hi', 'yo', 'sup'); $bye = array('bye', 'seeya', 'aurevoir'); $words = preg_replace('/\b('.implode('|', $hello).')\b/i', '<span class="highlight">'.array_rand($bye).'</span>', $words); echo $words; ?>

$index = array_rand($bye); echo $bye[$index];

php arrays random

No comments:

Post a Comment