Your code looks correct, but you can try it this way too:
<?php
$f_contents = file("random.txt");
$line = $f_contents[rand(0, count($f_contents) - 1)];
?>
More Related Contents:
- An algorithm for Random Number Pair Generation [closed]
- PHP random string generator
- How to generate a random, unique, alphanumeric string?
- Generating random results by weight in PHP?
- Secure random number generation in PHP
- Generating a random password in php
- How to get random value out of an array?
- Generating (pseudo)random alpha-numeric strings
- MySQL: Select Random Entry, but Weight Towards Certain Entries
- Laravel – Eloquent or Fluent random row
- How to extract text from the PDF document? [closed]
- Read a plain text file with php
- best practice to generate random token for forgot password
- How to create a random string using PHP?
- What is the best way to generate a random key within PHP?
- What’s the disadvantage of mt_rand?
- limit text length in php and provide ‘Read more’ link
- Efficiently pick n random elements from PHP array (without shuffle)
- Get Text From Tag Using PHP
- How to convert text to SVG paths?
- Difference between mt_rand() and rand()
- Algorithm for generating a random number
- Replace string in text file using PHP
- Random Float between 0 and 1 in PHP
- Generate cryptographically secure random numbers in php
- How random is PHP’s shuffle function?
- str_shuffle and randomness
- php random x digit number
- In PHP, how do I generate a big pseudo-random number?
- How to generate random password with PHP?