Quantcast
Channel: Developer Feed - Snippet
Viewing all articles
Browse latest Browse all 178

Display Random Quotes on Website using Php

$
0
0

Random Quotes in PHPSome website display random quotes to its visitors on various pages. The following snippet shows how to display a random quote(when ever the function is invoked) out of list of quotes that are pre populated into the arrary.

As a further extension the quotes can be loaded from a newline delimited file (one quote per line) or from a database and then stored into global variable array which can be passed into this get_random_quotes function to get the next random quote.


<?php

/**
* This simple snippet can be used to display
* a random quote.
*/


// define an array that lists out the quotes

$quotes=array(
'Enjoy Life',
'A liar always a liar',
'Nature learns',
'Live long',
'God is truth'
);

function get_random_quote($quotes)
{

// generate a random no between 0 and size of quotes array -1
// as the index starts with 0
$random_no= rand(0,sizeof($quotes)-1);

// get the quote corresponding to random no generated
$quote=$quotes[$random_no];

return$quote;
}


/**
* Usage
*/

echo get_random_quote($quotes);
?>

Viewing all articles
Browse latest Browse all 178

Trending Articles