Domanda

Can the following random_text function in PHP be done with a better method, e.g. Javascript, jQuery, SSI, etc.? I am looking for a method that is the quickest to render and is the least intensive on the server for faster page loads. Or, is PHP is the best method?

$random_text = array("Random Text 1",
                "Random Text 2",
                "Random Text 3",
                "Random Text 4",
                "Random Text 5");

print_r($random_text[array_rand($random_text)]);
È stato utile?

Soluzione

Do it using jQuery on the client side:

var result = random_text[Math.floor(Math.random()*random_text.length)];
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top