Welcome to WebmasterWorld Guest from 54.211.82.105

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

while loop problem!

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

Junior Member

5+ Year Member

joined:Nov 16, 2008
posts:136
votes: 0


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]

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

Junior Member

5+ Year Member

joined:Nov 16, 2008
posts:136
votes: 0


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);
?>
2:46 pm on Apr 19, 2011 (gmt 0)

Junior Member

5+ Year Member

joined:Feb 25, 2011
posts: 91
votes: 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.
8:09 pm on Apr 19, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:Feb 22, 2009
posts:1396
votes: 0


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
12:13 am on Apr 20, 2011 (gmt 0)

Junior Member

5+ Year Member

joined:Nov 16, 2008
posts:136
votes: 0


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