Jump to content

PHP Random Quotes


driz
 Share

Recommended Posts

I wrote this JS randomizer, and wanted to make it in PHP, any help?It needs to be as simple as the JS version. x

	<!--	var how_many_quotes = 6	var now = new Date()	var sec = now.getSeconds()	var quote = sec % how_many_quotes;	quote +=1;			if (quote==1){			title="One";		}			if (quote==2){			title="Two";		}			if (quote==3){			title="Three";		}		if (quote==4){			title="Four";		}		if (quote==5){			title="Five";		}		if (quote==6){			title="Six";		}							document.write('' + title + ' ');	-->

Link to comment
Share on other sites

Well, you know, Javascript can create a random number without the timer. And so can PHP.

<?php$quotes = array();$quotes[0] = "One";$quotes[1] = "Two";$quotes[2] = "Three";$quotes[3] = "Four";$quotes[4] = "Five";$quotes[5] = "six";$N = array_rand($quotes);$title = $quotes[$N];?>

Link to comment
Share on other sites

That could be further shortened to:

$quotes = array("One", "Two", "Three", "Four", "Five", "Six");$title = $quotes[array_rand($quotes)];

And you could also get your $quotes from a database.

Link to comment
Share on other sites

I'd have done it that way, but I figured it would be easier to read long quotes if each one were on its own line:...$quotes[0] = "Insert a long quote here";$quotes[1] = "Insert another long quote here";...

Link to comment
Share on other sites

Oh, I see. Then I would do this (still preserving the efficiency and some neatness):

$quotes = array(		"One",		"Two",		"Three",		"Four",		"Five",		"Six"	);

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...