homepage Welcome to WebmasterWorld Guest from 54.204.73.126
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
while loop problem!
skoff

5+ Year Member



 
Msg#: 4300407 posted 2:22 pm on Apr 19, 2011 (gmt 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]

 

skoff

5+ Year Member



 
Msg#: 4300407 posted 2:24 pm on Apr 19, 2011 (gmt 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);
?>

mbabuskov



 
Msg#: 4300407 posted 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

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 4300407 posted 8:09 pm on Apr 19, 2011 (gmt 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

skoff

5+ Year Member



 
Msg#: 4300407 posted 12:13 am on Apr 20, 2011 (gmt 0)

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved