FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Php generator. [help] [solved]





engammalsko
Hi, I new to php and tried to make a generator.

This is my code
Code:

<?php
$value = array("Carlos","James","Adam");
$value2 = array("Smith","Bush","Obama");

$rand=array_rand($value,2);
$rand2=array_rand($value2,2);

echo "You're name is: ".$value[$rand[0]]." ".$value2[$rand2[0]];
?>


So why does it just generate the 2 first values in array value and array value2?
My site URL: http://engammalsko.frihost.org/generate2.php

Edit: The code works in codepad.org...
Fire Boar
The second parameter of array_rand is unnecessary. What the code you posted outputs is a single line of text saying "You're name is: [something-from-$value] [something-from-$value2]". Is that what you were after?

If you are getting the same result each time, it is probably because your PHP version is less than 4.2.0. Check your version by putting the following in a PHP script:

Code:
<?php echo phpversion(); ?>


If it is less than 4.2.0, you need to call the srand function before calling rand or array_rand. This should do the trick:

Code:
<?php srand(str_replace(' ', '', microtime())); ?>


(Oh, by the way, it's "your". "You're" is the contraction of "you are", so the sentence expands to "You are name is:" which doesn't make sense.)
engammalsko
My version is 5.2.17.

So the code should work?
Like I said before, I'm new to php so this is the only way I know how to do this atm.

And yeah, I didn't pay attention to what I wrote because I was in class and just made a quick translation to English to make my function easier to understand.
Fire Boar
Ah, it seems that array_rand works slightly differently to how I thought. The second parameter is more important than I thought. It basically generates that many UNIQUE values from the array, ordering them by key. So...

Code:
$values = array("A", "B", "C");
$rand1 = array_rand($value); // 0 or 1 or 2, 33% chance of each.
$rand2 = array_rand($value, 2); // array(0, 1) or array(0, 2) or array(1, 2), 33% chance of each.
$rand3 = array_rand($value, 3); // array(0, 1, 2) always.


In the $rand2 case above, which resembles your code, you get a 2/3 chance of $rand2[0] being 0, and a 1/3 chance of $rand2[0] being 1. It will never be 2. That's your problem then, and your code should look like this instead:

Code:
<?php
$value = array("Carlos","James","Adam");
$value2 = array("Smith","Bush","Obama");

$rand = array_rand($value);
$rand2 = array_rand($value2);

echo "Your name is: ".$value[$rand]." ".$value2[$rand2];
?>
engammalsko
I just solved it with my friends code. x)

But thanks for all the help!
Related topics
PHP installtion Help!
php script help!
PHP Layout Help -- URGENT
Uploading a .php file! NEED HELP thanks
PHP-Nuke.. help!
Uplaoding images using php form :::: Help needed
PHP Nuke Help
PHP + GD coder needed - Dynamic Sig Help {SOLVED}
PHP Scripting HELP
PHP beginner help!!!
Templates to the php forum help me
A Little PHP GD Help...
PHP sig help[solved]
php project help needed please
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.