从文本文件中获取随机行

时间:2015-09-13 11:31:50

标签: php arrays file

我希望在用户刷新页面时调用random.txt中的随机单词,但我只获得$word_array[0]而不是任何其他$ word_array [1..3]。

random.txt

hello
how
are
you

PHP代码:

myfile = fopen("random.txt", "r") or die("Unable to open file!");
$word_array = array(fgets($myfile));

$word = rand(0,4);
$lines[] = fgets($myfile);
echo $word_array[$word];

fclose ($myfile); 

错误是什么?

更新:如果可以避免循环,并仅纠正此代码。

0 个答案:

没有答案
相关问题