6/03/10
PHP Text Rotator

php text rotator There are many ways to produce a text rotating effect on a webpage. What this effect details is the random changing of content or text every time the page is loaded. You can achieve this effect by using javascript or many other web scripting languages in use today, however I chose to use php for my rotator simply because of its reliability. Since php is executed on the server, you don't have to worry at all about the user's browser not supporting the script. The script is pretty straightforward, there arn't any special tricks in it that somebody with a firm knowledge of php programming wouldn't already know. But simplicity is sometimes the best way to approach something in a programming sense.

 

Features

  • Program: Wamp Server, notepadd++
  • Difficulty: Basic (base html knowledge required)
  • Estimated Completion Time: 10 min.
  • Author: Matt Rheault

 

What we will be doing to create this script is creating a function containing an array that will contain the content or text to be replaced. We will then parse this array through the array_rand() method, which will randomly decide which item in the array to display. The last bit is of course to echo the results where ever the function is called. Below is what the php code will look like

 

<?php
function rotator() {
	$rotatorText = array(
		'Who knew php could be so easy?',
		'That\'s all folks!',
		'Insert witty slogan here...',
		'Man this is a large footer...',
		'This site took 42 cups of coffee to produce.',
		'Something doesn\'t seem right...',
		'I hope you enjoyed your stay!',
		'Come back soon!');
	$rand_key = array_rand($rotatorText);
	echo $rotatorText[$rand_key];
}
?>

The final step

Now that we have our function written, its time to call it up.

<?php rotator(); ?>

 

So there you have it! No really... thats all you have to do. Pretty simple huh? Its amazing the possibilities of using such a technique. For instance you could randomly interchange style sheets, images, or anything else you could ever want to randomly display on your webpage. All in all its a pretty simple technique that can really add quite a dynamic feel to your site. I actually use this exact script in my footer; if you notice every time you reload a page on my site it will say something different. I put the index.php file from the demo up for download if anybody wants to take a look at it themselves. One quick note with the demo, yes it is written in html5 and yes it does use css3 gradients, enjoy!

Options:

 
Need a job done?

To hire me, visit my contact page and let me know about what you need done.
Here is a list of what I do:

  • Business Cards
  • Logo Design
  • Website Design
  • Website Maintenance/Programming
  • Anything else, just ask :)
Theme updated on 08th April, 2011 @ 09:24am
site loaded in...0.92 seconds
preload preload preload preload preload preload preload preload preload preload