php fail again : -------- (

well after reading some tutorials I ve wrote the following programm which should get 7 random generated numbers out of 49 without taking one twice, but somehow I got a loop to infinity : (

Solved





For those who want to play Lotto themself =
Quote
<?php

$arr = array(49);

for ($k = 0; $k<49; $k++)
{
$arr[$k] = false;
}

while ($i < 6)
{
$zufall = rand(1,49);
if ($arr[$zufall] == false)
{
$i++;
echo 'Die '.$i.'. Zahl lautet: '.$zufall.'<br>';
$arr[$zufall] = true;
}
}

while ($i < 8 and $i = 7)
{
$zufall = rand(1,49);
if ($arr[$zufall] == false)
{
$i++;
echo 'Die Zufallszahl lautet: '.$zufall.'<br>';
}
}
?>


Comments
21
Well thought about that also but then I decided it cant be the problem, thx anyways.
Parent
Found the mistake myself :D
while ($i < 8 and $i = 7)

huh... syntax errors aside... what.
well the only error was that I made a ! infront the If function :-(
Parent
why didn't you highlight the = ? :P
Parent
Quotesyntax errors
Parent
yeah but there was no need for the second line, all you had to do with bold them both
Parent
whats wrong with the = ? :/
Parent
"$i = 7" is always true :)
use "==" for comparing
Parent
3 x 3 = 9
wow, making a simple problem difficult...

Quote
$randomNumbers = array();
for($i = 0; $i < 7; $i++) {
do {
$number = rand(1, 49);
} while(in_array($number, $randomNumbers));
array_push($randomNumbers, $number);
}
// now there are 7 different numbers between 1 and 49 in the array $randomNumbers, print it or so
print_r($randomNumbers);
array_push was my first thought too
Parent
the first few lines of his code could also be replaced by only one:
$arr = array_fill(1, 49, false);
Parent
not gonna follow the "use $array[] instead if only adding one element"?
Parent
well I just read some tutorial without all those special commands you are listing here. :/
Parent
php.net & lol @ special comamnds x)
Parent
Überlass dein Glück im Lotto niemals einem Script! :D
thaaaat's amazing...........NOT!
Back to top