Welcome to WebmasterWorld Guest from 54.227.83.19

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

while loop problem!

     

skoff

2:22 pm on Apr 19, 2011 (gmt 0)

5+ Year Member



Hi everyone!
My problem is that i generate random number between 0 and 1 for home team and away team. I want this to generate until home and away are not equal. So here's the code i made but it goes forever.. it never stop! I'm missing something but i dont know what?

<?php
$home=rand(0, 1);
$away=rand(0, 1);
do
{
echo "HOME : " . $home . " || AWAY : " . $away . "<br />";
}
while ($home == $away);
?>

Thanks!

[edited by: skoff at 2:27 pm (utc) on Apr 19, 2011]

skoff

2:24 pm on Apr 19, 2011 (gmt 0)

5+ Year Member



i found my problem while reading what i posted.. i had to include my variables in my "do".. Am I right?


<?php
do
{
$home=rand(0, 1);
$away=rand(0, 1);
echo "HOME : " . $home . " || AWAY : " . $away . "<br />";
}
while ($home == $away);
?>

mbabuskov

2:46 pm on Apr 19, 2011 (gmt 0)



Almost. Now you are iterating as long as they are equal. In your message you wrote that you want to iterate until they are equal, so you need to use != comparison instead.

Matthew1980

8:09 pm on Apr 19, 2011 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



Have a read of the manual to see what the done thing is: Link here [php.net]

I guess as your just trying to make this look more 'grammer styled lingo in here:-

echo "HOME : " . $home . " || AWAY : " . $away . "<br />";

I do this sort of thing in comments, but not of anything else, it's kinda shorthand, but personally I wouldn't advocate doing this as it can easily confuse people. Stick to the or not ||. Thats just my 10p worth.

Cheers,
MRb

skoff

12:13 am on Apr 20, 2011 (gmt 0)

5+ Year Member



its only to see if it works im not really showing the result this way...
 

Featured Threads

Hot Threads This Week

Hot Threads This Month