Kristian_C Posted December 21, 2006 Share Posted December 21, 2006 Hey, lets say i click a link, or a button.How can i etc get : hey the frist time then hello ?tried with this code : if ($_POST[test]){$test=rand(0,3);$test = array('Hello','Hey','Test');echo"$test"; but dident work...anyone that know it? Link to comment Share on other sites More sharing options...
justsomeguy Posted December 21, 2006 Share Posted December 21, 2006 There are a few things wrong. First, you should refer to something inside an array (such as $_POST) in quotes. If you leave out the quotes, the parser assumes you are trying to use a defined constant. Other then that, see what I did and maybe you can see what you did wrong: if ($_POST['test']){$nr=rand(0,2);$test = array('Hello','Hey','Test');echo $test[$nr]; Also, since there are three elements in the array, the random number should go from 0 to 2, not 0 to 3. The three array elements are 0, 1, and 2. 3 is not in there. Link to comment Share on other sites More sharing options...
Kristian_C Posted December 21, 2006 Author Share Posted December 21, 2006 Aha, i think i can see what i did wrong .. dident add the "select random" or what i can name it.. like $test[$nr]. the : [] counts the random tight? or not counts but selects or something? hehe.. thanks for helping again and again... Link to comment Share on other sites More sharing options...
Sniffy Posted December 21, 2006 Share Posted December 21, 2006 Thanks for posting the question, now I know how to make a random value in PHP. Link to comment Share on other sites More sharing options...
justsomeguy Posted December 21, 2006 Share Posted December 21, 2006 Aha, i think i can see what i did wrong .. dident add the "select random" or what i can name it.. like $test[$nr]. the : [] counts the random tight? or not counts but selects or something? hehe..You can think of an array in PHP as a list. The array you created here is a list with 3 items in it, the items are the strings "Hello", "Hey", and "Test". Items in an array are also referred to as elements. Each element has an index or key that you can use to refer to it. If "Hello" is the first element in the array $test, then you can refer to it with $test[0]. If "Test" is the third element, then you can refer to it as $test[2]. Arrays are zero-based, meaning that the first element is element 0, not element 1. So, an array with n elements will be numbered 0 to n-1. An array with 10 elements will have keys 0-9.There are also associative arrays, which are arrays that have names for indexes instead of numbers. So you can create an array like this:$person = array("name" => "Steve", "age" => 27, "state" => "dazed");And you can refer to those elements as $person['name'], $person['age'], etc.If you want to get a random element from an array, you can use this function:http://www.php.net/manual/en/function.array-rand.php$random_array_element = array_rand($test_array); Link to comment Share on other sites More sharing options...
Kristian_C Posted December 21, 2006 Author Share Posted December 21, 2006 Hehe, thanks .. feel that i can learn alot from you ... and it explains why i got a zero named echo when i had 12 items and 0,12 random hehe.. Link to comment Share on other sites More sharing options...
Sniffy Posted December 21, 2006 Share Posted December 21, 2006 $person = array("name" => "Steve", "age" => 27, "state" => "dazed");Yeah, I find associative arrays very useful, but I always called them literal arrays, is that correct? Link to comment Share on other sites More sharing options...
justsomeguy Posted December 21, 2006 Share Posted December 21, 2006 I've only heard them referred to as associative. Link to comment Share on other sites More sharing options...
Sniffy Posted December 22, 2006 Share Posted December 22, 2006 Okay, just checking. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now