Jump to content

Total PHP Newbie seeking help


nikifi

Recommended Posts

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 />";

 

 

 

}

 

 

?>

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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 />";
		  

}


?>

Link to comment
Share on other sites

<?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??

Link to comment
Share on other sites

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 :-)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.