PHP: Random Rotation

If you’re looking for a simple button rotation-type script, here’s one you can use! Note that this only works for pages with a .php extension.

You can try the random link rotation, random image rotation, or random linked image rotation (best for button rotations).

In all these rotations, the links, images or buttons will rotate when the page is refreshed.

Random Link Rotation

Copy the following code and paste it where you would like the link to appear.

<?
$linkvar=rand(1,2);
if($linkvar==1) {
	print '<a href="URL" target="_blank">TEXT LINK</a>';
}
else if($linkvar==2) {
	print '<a href="URL" target="_blank">TEXT LINK</a>';
}
?>

Fill in the URL and “TEXT LINK” – what you would like the link to read.

There is room in the above code for two links. For more links, copy the following lines:

else if($linkvar==#) {
	print '<a href="URL" target="_blank">TEXT LINK</a>';
}

Then paste them after the second link – directly before ?>. Change the # to the next number.

Also, in (1,2) – change the second number to the TOTAL number of links you have.

Random Image Rotation

Copy the following code and paste it where you would like the image to appear.

<?
$linkvar=rand(1,2);
if($linkvar==1) {
	print '<img src="URLOFIMAGE" height="HEIGHT" width="WIDTH" alt="ALT">';
}
else if($linkvar==2) {
	print '<img src="URLOFIMAGE" height="HEIGHT" width="WIDTH" alt="ALT">';
}
?>

Fill in the URL, height, width and alternative text of the image. The URL must be the exact and full URL, eg. http://yoursite.com/folder/image.jpg.

If you’re using XHTML – don’t forget the backslash before the > in the image tag.

There is room in the above code for two images. For more images, copy the following lines:

else if($linkvar==#) {
	print '<img src="URLOFIMAGE" height="HEIGHT" width="WIDTH" alt="ALT">';
}

Then paste them after the second image – directly before ?>. Change the # to the next number.

Also, in (1,2) – change the second number to the total number of images you have.

Random Linked Image Rotation

Copy the following code and paste it where you would like the image to appear.

<?
$linkvar=rand(1,2);
if($linkvar==1) {
	print '<a href="URL" target="_blank"><img src="URLOFIMAGE" height="HEIGHT" width="WIDTH" alt="ALT" border="0"></a>';
}
else if($linkvar==2) {
	print '<a href="URL" target="_blank"><img src="URLOFIMAGE" height="HEIGHT" width="WIDTH" alt="ALT" border="0"></a>';
}
?>

Fill in the URL, and the URL, height, width and alternative text of the image. The URL must be the exact and full URL, eg. http://yoursite.com/folder/image.jpg.

If you’re using XHTML – don’t forget the backslash before the > in the image tag.

There is room in the above code for two linked images. For more linked images, copy the following lines:

else if($linkvar==#) {
	print '<img src="URLOFIMAGE" height="HEIGHT" width="WIDTH" alt="ALT">';
}

Then paste them after the second linked image – directly before ?>. Change the # to the next number.

Also, in (1,2) – change the second number to the total number of linked images you have.