A few questions
Moderator: General Moderators
A few questions
I dont know much PHP, but i'm good at picking it up and understanding it most times. You can either tell me how to do this or do it for me, i dont care.
I'm wanting to make something with PHP, that writes 1000 lines, each saying "bob 1" without the quotes. And on the second line the 1 would change to a 2 and so on.......
the word bob is just an example, it will say something else.
My PHP book that i have doesnt tell me how to write 1 thousand lines with a counting number on each one, so thats why i'm asking here.
I'm wanting to make something with PHP, that writes 1000 lines, each saying "bob 1" without the quotes. And on the second line the 1 would change to a 2 and so on.......
the word bob is just an example, it will say something else.
My PHP book that i have doesnt tell me how to write 1 thousand lines with a counting number on each one, so thats why i'm asking here.
Code: Select all
for($i=0;$i<1000;$i++) {
echo "Bob $i <br />";
}sweet thanks.
Now how can i make it randomly choose between sf, at4, ak, ak74su, rpk, svd, mos, spr, rpg, s, s24, gp, g, d, and bdm and randomly display one of those after each line?
BTW, this whole thing togethor will make a big long page showing a thousand commands that do something in a program i have.
Thanks.
Now how can i make it randomly choose between sf, at4, ak, ak74su, rpk, svd, mos, spr, rpg, s, s24, gp, g, d, and bdm and randomly display one of those after each line?
BTW, this whole thing togethor will make a big long page showing a thousand commands that do something in a program i have.
Thanks.
http://www.php.net/count to count the number of avaible thingies
http://www.php.net/rand to get a random number
combine the two, and you can get a random number, which acts as the index, and thus gives you a random element of $thingies.
http://www.php.net/rand to get a random number
combine the two, and you can get a random number, which acts as the index, and thus gives you a random element of $thingies.
Code: Select all
$suffixes=array('sf','at4','ak','gp','rpk','svd','s','spr');
for($i=1;$i<=1000;$i++)
{
$randValue=rand(0,count($suffixes)-1);
echo "Bob {$i} {$suffixes[$randValue]} <br />";
}Code: Select all
for($i=1;$i<=1000;$i++)Code: Select all
for($i=0;$i<1000;$i++)
Last edited by trdesigns on Wed Aug 03, 2005 1:34 pm, edited 1 time in total.
Code: Select all
<?php
$suffixes=array('sf','at4','ak','gp','rpk','svd','s','spr');
for($i=1;$i<=1000;$i++)
{
$randValue=rand(0,count($suffixes)-1);
echo "Bob {$i} {$suffixes[$randValue]} <br />";
}
?>Here's a version that uses array_rand() and is, in my opinion, simpler and easier to understand.
I'm not sure what the curly braces do inside double quotes - I've never used them so I took them out. array_rand(), obviously, chooses a random element from the array.
Remember that complete randomness doesn't eliminate the possibility of having the same value line after line.
Edit: I updated line 7 of my code to be correct.
Code: Select all
<?php
$suffixes=array('sf','at4','ak','gp','rpk','svd','s','spr');
for($i=1;$i<=1000;$i++)
{
$rand_suffix = $suffixes[array_rand($suffixes)];
echo "Bob $i $rand_suffix <br />";
}
?>Remember that complete randomness doesn't eliminate the possibility of having the same value line after line.
Edit: I updated line 7 of my code to be correct.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.