nikifi Posted November 24, 2009 Share Posted November 24, 2009 Hello Folks, I am brand new to php and is reading PHP 6/MySQL Programming for the Absolute Beginner in an attempt to teach myself php. I have a question about a basic problem that I somehow cant figure out. I dont understand why the following code prints nothing (blank page). Any help/explanation will be greatly appreciated. Thanks <?php $card = array(); for ($i= 1; $i <= 52; $i++) { $card = rand(1,52); print "$card[1]<br />"; print "$card[2]<br />"; print "$card[3]<br />"; print "$card[4]<br />"; print "$card[5]<br />"; } ?> Quote Link to comment Share on other sites More sharing options...
Alex Posted November 24, 2009 Share Posted November 24, 2009 $card = rand(1,52); rand returns a random number, this just makes $card a variable that holds a number 1-52, so the elements $card[1], $card[2], etc.. don't exist. Quote Link to comment Share on other sites More sharing options...
nikifi Posted November 24, 2009 Author Share Posted November 24, 2009 Thanks for your responce, but I mistyped. My code really looks like this: <?php $card = array(); for ($i= 1; $i <= 52; $i++) { $card = rand(1,52); print "$card[1]<br />"; print "$card[2]<br />"; print "$card[3]<br />"; print "$card[4]<br />"; print "$card[5]<br />"; } ?> now its not $card is set to rand() its card...Doesn't that make a difference? Thanks! Quote Link to comment Share on other sites More sharing options...
sawade Posted November 24, 2009 Share Posted November 24, 2009 Both snips of code look the same... Please use code tags. Thanks. Quote Link to comment Share on other sites More sharing options...
nikifi Posted November 24, 2009 Author Share Posted November 24, 2009 Sorry about that...Here is the code: <?php $card = array(); for ($i= 1; $i <= 52; $i++) { $card[i] = rand(1,52); print "$card[1]<br />"; print "$card[2]<br />"; print "$card[3]<br />"; print "$card[4]<br />"; print "$card[5]<br />"; } ?> Quote Link to comment Share on other sites More sharing options...
Deoctor Posted November 24, 2009 Share Posted November 24, 2009 <?php $card = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18); for ($i= 1; $i <= 18; $i++) { $card[i] = rand(1,18); print "$card[1]<br />"; print "$card[2]<br />"; print "$card[3]<br />"; print "$card[4]<br />"; print "$card[5]<br />"; } ?> try this it should print a number between 1 -18 it will print u 2-6 but i dont got it why u are wriitng this.. do u want to pick a random number from the array?? Quote Link to comment Share on other sites More sharing options...
Deoctor Posted November 24, 2009 Share Posted November 24, 2009 if u want to do so u can use this code instead <?php $card = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18); $i= rand(1,18); echo "$card[$i]"; echo "<p>Hello</p>"; ?> Quote Link to comment Share on other sites More sharing options...
nikifi Posted November 24, 2009 Author Share Posted November 24, 2009 Thanks for your reply. See, the problem is I am trying to teach myself to generate arrays that do not require me to define each of them, such as in the case if I had a 10,000 array. So, I was trying to use a for loop to do that. In this case I really want to generate 5 random cards but first I am using text then if I get that right I can show the card images. So, I was trying to create an array with 52 slots, then I want to pick a random 5. I know I can assign the values 1 - 52 but I know this wouldn't be practical for very large amounts, so I am trying to use a loop to do the trick. However the trick isn't working :-) Quote Link to comment Share on other sites More sharing options...
Deoctor Posted November 24, 2009 Share Posted November 24, 2009 is the below code not working?? Quote Link to comment Share on other sites More sharing options...
nikifi Posted November 24, 2009 Author Share Posted November 24, 2009 Here is the revisd ode that does exactly what I wanted: <?php $card = array(); for ($i= 1; $i <= 5; $i++) { $card[i] = rand(1,52); print "$card[i]<br />"; } ?> Thanks again! Quote Link to comment Share on other sites More sharing options...
Deoctor Posted November 24, 2009 Share Posted November 24, 2009 dear nikifi The below code will result in picking up 5 random numbers from 1-52 and display it off one by one.. till the count becomes five.. Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted November 24, 2009 Share Posted November 24, 2009 Here is the revisd ode that does exactly what I wanted: <?php $card = array(); for ($i= 1; $i <= 5; $i++) { $card[i] = rand(1,52); print "$card[i]<br />"; } ?> Thanks again! i'm thinking you meant to have: $card[$i] = rand(1,52); and not: $card[i] = rand(1,52); as the latter of the two is just creating an index of i, and not actually setting the index of the array as the incremental number that is being stored in $i. you can accomplish this without an array since you're not really doing it right anyways: <?php for ($i=1; $i<=5; $i++) { echo rand(1,52).'<br />'; } ?> does the exact same thing. no need to mess with arrays in this case. Quote Link to comment 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.