dreamwest Posted April 25, 2009 Share Posted April 25, 2009 What is a simple way of by using php to create a random number based on a array, i can do this with javascript but i need to convert it to php: Javascript random array: <SCRIPT LANGUAGE="Javascript"> function text() { }; text = new text(); number = 0; // textArray text[number++] = "3894" text[number++] = "2236" text[number++] = "4976" // keep adding items here... increment = Math.floor(Math.random() * number); document.write(text[increment]); //--></SCRIPT> Quote Link to comment https://forums.phpfreaks.com/topic/155600-solved-number-array/ Share on other sites More sharing options...
gizmola Posted April 25, 2009 Share Posted April 25, 2009 Set up a php array. Depending on the number of elements, you can use rand() to return one at random. For example, if you created a 20 element array: $text[] = 3849; $text[] = 2238; // etc. echo $text[rand(1, 20)]; Quote Link to comment https://forums.phpfreaks.com/topic/155600-solved-number-array/#findComment-818923 Share on other sites More sharing options...
dreamwest Posted April 25, 2009 Author Share Posted April 25, 2009 Thanks. Was the rand thing i didnt know about $text[0] = 1; $text[1] = 2; $text[2] = 3; $text[3] = 4; $text[4] = 5; // etc. echo $text[rand(0, 4)]; Quote Link to comment https://forums.phpfreaks.com/topic/155600-solved-number-array/#findComment-818929 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.