Is shuffling an effective method to randomly select content from a text file in PHP?

Shuffling an array is an effective method to randomly select content from a text file in PHP. By reading the contents of the text file into an array and then shuffling the array, you can easily randomize the order of the content and select items randomly.

// Read the contents of the text file into an array
$lines = file('example.txt', FILE_IGNORE_NEW_LINES);

// Shuffle the array to randomize the order of the content
shuffle($lines);

// Select a random line from the shuffled array
$randomLine = $lines[array_rand($lines)];

echo $randomLine;