Zola Posted January 19, 2012 Share Posted January 19, 2012 Hi, I am wanting to create a random fact script for a small website project. The idea is when the user loads a page a random fact will appear at the top ( I guess from an array of strings I will make?) Can anyone recommend the most efficient and easiest way to go about this please? Link to comment https://forums.phpfreaks.com/topic/255354-random-text-load-script/ Share on other sites More sharing options...
Zola Posted January 19, 2012 Author Share Posted January 19, 2012 I found this code, can someone explain it a little to me please? <?php $random_text = array("Random Text 1", "Random Text 2", "Random Text 3", "Random Text 4", "Random Text 5"); $sizeof = count($random_text); $random = (rand()%$sizeof); print("$random_text[$random]"); ?> Why do we need the $sizeof and count? Link to comment https://forums.phpfreaks.com/topic/255354-random-text-load-script/#findComment-1309196 Share on other sites More sharing options...
dzelenika Posted January 19, 2012 Share Posted January 19, 2012 count() — Count all elements in an array rand — Generate a random integer $random = (rand()%$sizeof limits generated number 0 to $sizeof Link to comment https://forums.phpfreaks.com/topic/255354-random-text-load-script/#findComment-1309197 Share on other sites More sharing options...
Zola Posted January 19, 2012 Author Share Posted January 19, 2012 Sorry for the noob questions, but I am learning all about this stuff. Thanks very much Link to comment https://forums.phpfreaks.com/topic/255354-random-text-load-script/#findComment-1309199 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.