Create Random Text in PHP

by Tweak on June 16, 2009
in Php

Want to put a random quote on your site? Or randomize some other aspect of your site? It’s easy. You’ll want to use PHP or some other server-side scripting language instead of a client-side scripting language like JavaScript. Server-side languages write the content to the page in html before it ever leaves the server. Search engines will be able to read the content on your page, but with JavaScript they cannot, so you lose any content value of the random code. Also, if a user turns off JavaScript, that random content is lost. Google, MSN, and Yahoo will see that the content is changing often, and that may help your rankings (depending on your implementation).

Here’s how to put the random elements into your page, using PHP.

Step 1:
Create a text file called ‘myData.txt’. Open the file and put in your random elements, placing each random element on its own line, (whether text, images or html). Then place ‘myData.txt’ in the same directory as your page containing the PHP script.

Step 2:
Place the following PHP code into your page where you want the random element:
Read more…