1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

help me code a simple script

Discussion in 'PHP & Perl' started by sussah, Jan 8, 2012.

  1. sussah

    sussah Newbie

    Joined:
    Dec 1, 2011
    Messages:
    37
    Likes Received:
    2
    i want to write pretty simple code.


    basically display a random line taken from a .txt file on a webpage.

    example:

    note pad file looks like this

    123
    abc
    456
    def

    webpage displays "123", then if refreshed randomly chooses "def", and so on.
     
  2. chatmasta

    chatmasta Junior Member

    Joined:
    Sep 1, 2007
    Messages:
    122
    Likes Received:
    38
    PHP:
    <?php
    $filename 
    '/path/to/file.txt';

    $lines file($filename);
    $random_line $lines[array_rand($lines)];

    echo 
    $random_line;

    ?>
     
    • Thanks Thanks x 1
  3. sussah

    sussah Newbie

    Joined:
    Dec 1, 2011
    Messages:
    37
    Likes Received:
    2
    Thanks man, flawless