Re: Displaying Text From A Data File

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



"Ron Piggott" <ron.php@xxxxxxxxxxxxxxxxxx> wrote in message
news:001b01c492a9$275ab1c0$95703ad8@xxxxxx
> I am still new to PHP and I wanted to ask a question.
>
> I am wanting to create a PHP script to randomly display one of many
greeting
> messages on my web site.  I was going to store them in a text based file.
I
> wanted PHP to randomly select one of them and display it each time the
> script was loaded.
>
> I am still new at PHP and I am not sure where to get started.  But I was
> thinking about this and I am sure this has already been done many times
...
> I am not sure where to look on the web.
>
> Can any of you help me get started?
>
> Ron

Hi Ron,

put your messages in a text file line by line (each message on a separate
line). Then use the following code to echo out a random message:

$messages  = file('/path/to/your/file.txt'); // file() returns an array
where each line is an array element
$randomKey = array_rand($messages); // will return a random array index
echo $messages[$randomKey];

Hope this helps.

Regards, torsten Roehr

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux