You want to randomly reorder all lines in a file. You have a file of funny quotes, for example, and you want to pick out one at random.
Read all the lines in the file into an array with file() and then shuffle the elements of the array:
1 2 3 4 5 6 7 |
$lines = file(__DIR__ . '/quotes-of-the-day.txt'); if (shuffle($lines)) { // okay } else { die("Failed to shuffle"); } |
The shuffle() function randomly reorders the array elements, so after shuffling, you can pick out $lines[0] as a quote to display.