Random Text

This is just a variation on the Random Quote function. I used it to add different NASCAR driver names to some text on my NASCAR racing blog and database web site.

Reload this page to see the random text changer in action changing the driver names.

  • Know how many laps Bobby Labonte completed in 2005?
  • The average finish for Greg Biffle on oval, quad-oval or road courses?
  • How many poles Kurt Busch has won?

Using this script on your site.

I use simple scripts like this as custom functions included in the head of my web page. Then, I can call the function without a lot of extra markup involved. Keeps my pages leaner and cleaner.

So, for this example, I would save the php code as a page called 'randomDriver.inc.php' and put it in a folder I call 'includes'.

Remember, you'll have to save your web page with the '.php' extension to get the script to work!

PHP code for Random Text function

<?php
// this function displays random text in a sentence

function randomDriver() {

$drivers = array(
0 => 'Greg Biffle',
1 => 'Kyle Busch',
2 => 'Jeff Burton',
3 => 'Kurt Busch',
4 => 'Dale Earnhardt Jr',
5 => 'Carl Edwards',
6 => 'Robby Gordon',
7 => 'Jeff Gordon',
8 => 'Kevin Harvick',
9 => 'Dale Jarrett',
10 => 'Jimmie Johnson',
11 => 'Kasey Kahne',
12 => 'Matt Kenseth',
13 => 'Bobby Labonte',
14 => 'Mark Martin',
15 => 'Jamie McMurray',
16 => 'Jeremy Mayfield',
17 => 'Casey Mears',
18 => 'Ryan Newman',
19 => 'Kyle Petty',
20 => 'Elliott Sadler',
21 => 'Ken Schrader',
22 => 'Tony Stewart',
23 => 'Brian Vickers',
24 => 'Michael Waltrip');

srand ((double) microtime() * 1000000);
$random_number = rand(0,count($drivers)-1);

$driver = ($drivers[$random_number]);
return $driver;
?>

HTML for Random Text function

<?php
include 'includes/randomQuote.inc.php';
?>
<html>
<body>

<p>Lorem ipsum qui.....</p>
<ul>
<li>Know how many laps <?php echo randomDriver(); ?> completed in 2005?</li>
<li>The average finish for <?php echo randomDriver(); ?> on oval, quad-oval or road courses?</li>
<li>How many poles <?php echo randomDriver(); ?> has won?</li>
</ul>

<body>
<html>

I also have a random image script and random quote script as well.