How to echo random line from text file

file, php

Solution

A generally good approach to this kind of situation is to:

- Read the lines into an array using `file()`

- `echo` out a random array value using `array_rand()`

Your code could look something like this:

$lines = file('my_file.txt');

echo $lines[array_rand($lines)];

Problem

My text file format is : This is first line. This is second line. This is third line. There could be more lines in the text file. How to echo one random line on each refresh from the text file with php. All comments are appreciated. Thanks

Original source

Related problems