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
https://forums.phpfreaks.com/topic/182740-total-php-newbie-seeking-help/
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!

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

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.