You are viewing our Forum Archives. To view or take place in current topics click here.
SOLVED: PHP infinite While loop help
Posted:

SOLVED: PHP infinite While loop helpPosted:

Orry
  • Ladder Climber
Status: Offline
Joined: Mar 01, 201113Year Member
Posts: 358
Reputation Power: 24
Status: Offline
Joined: Mar 01, 201113Year Member
Posts: 358
Reputation Power: 24
Hi I am trying to work out this problem where I need to make a infinite while loop generate random numbers between 1-10, where if the value is 5 it terminates, however any other number will output the cube root of that value.

This is what I have so far however I am new to PHP so I really don't know where to go from here to get the correct answer. Can anyone help me out?

while(true)
{
$counter = rand(1, 10);   
    if ($counter <=10) {
        echo sqrt($counter) . "</br>";
    }
if($counter ==5) break;
}


Last edited by Orry ; edited 1 time in total
#2. Posted:
Ryzen
  • Resident Elite
Status: Offline
Joined: May 16, 20176Year Member
Posts: 232
Reputation Power: 72
Status: Offline
Joined: May 16, 20176Year Member
Posts: 232
Reputation Power: 72
while(true) {
   $i = rand(1, 10);
   if($i != 5) {
      echo sqrt($i);
   }
}

This would be how I'd do it.
#3. Posted:
Orry
  • Ladder Climber
Status: Offline
Joined: Mar 01, 201113Year Member
Posts: 358
Reputation Power: 24
Status: Offline
Joined: Mar 01, 201113Year Member
Posts: 358
Reputation Power: 24
Ryzen wrote
while(true) {
   $i = rand(1, 10);
   if($i != 5) {
      echo sqrt($i);
   }
}

This would be how I'd do it.


There is a syntax error on that code for me bro. Around the rand function.
#4. Posted:
Orry
  • Ladder Climber
Status: Offline
Joined: Mar 01, 201113Year Member
Posts: 358
Reputation Power: 24
Status: Offline
Joined: Mar 01, 201113Year Member
Posts: 358
Reputation Power: 24
Ori wrote
Ryzen wrote
while(true) {
   $i = rand(1, 10);
   if($i != 5) {
      echo sqrt($i);
   }
}

This would be how I'd do it.


There is a syntax error on that code for me bro. Around the rand function.


Okay I fixed the syntax, however with your code, the web browser is very slow because of the loop. Is there any way to make it hit 5 quicker to terminate?
#5. Posted:
Ryzen
  • Resident Elite
Status: Offline
Joined: May 16, 20176Year Member
Posts: 232
Reputation Power: 72
Status: Offline
Joined: May 16, 20176Year Member
Posts: 232
Reputation Power: 72
Shouldn't be. I tested it and it works fine lol.
[ Register or Signin to view external links. ]
Only thing different is I added the <br/> to make it on a different line.
#6. Posted:
Ryzen
  • Resident Elite
Status: Offline
Joined: May 16, 20176Year Member
Posts: 232
Reputation Power: 72
Status: Offline
Joined: May 16, 20176Year Member
Posts: 232
Reputation Power: 72
Ori wrote
Ori wrote
Ryzen wrote
while(true) {
   $i = rand(1, 10);
   if($i != 5) {
      echo sqrt($i);
   }
}

This would be how I'd do it.


There is a syntax error on that code for me bro. Around the rand function.


Okay I fixed the syntax, however with your code, the web browser is very slow because of the loop. Is there any way to make it hit 5 quicker to terminate?

OH I miss understood what you were asking for haha. One second I'll fxi my code up.

Edit:
This would work
<?php
$a = true;
while($a == true) {
   $b = rand(1, 10);
   if($b != 5) {
      echo sqrt($b) .'<br/>';
   }
   else {
      $a = false;
   }
}
?>
#7. Posted:
Orry
  • Ladder Climber
Status: Offline
Joined: Mar 01, 201113Year Member
Posts: 358
Reputation Power: 24
Status: Offline
Joined: Mar 01, 201113Year Member
Posts: 358
Reputation Power: 24
Ryzen wrote
Ori wrote
Ori wrote
Ryzen wrote
while(true) {
   $i = rand(1, 10);
   if($i != 5) {
      echo sqrt($i);
   }
}

This would be how I'd do it.


There is a syntax error on that code for me bro. Around the rand function.


Okay I fixed the syntax, however with your code, the web browser is very slow because of the loop. Is there any way to make it hit 5 quicker to terminate?

OH I miss understood what you were asking for haha. One second I'll fxi my code up.

Edit:
This would work
<?php
$a = true;
while($a == true) {
   $b = rand(1, 10);
   if($b != 5) {
      echo sqrt($b) .'<br/>';
   }
   else {
      $a = false;
   }
}
?>


Damn thanks bro. Im pretty sure the code is fixed now. You're a genius haha. How good are you with PHP?
#8. Posted:
Ryzen
  • Resident Elite
Status: Offline
Joined: May 16, 20176Year Member
Posts: 232
Reputation Power: 72
Status: Offline
Joined: May 16, 20176Year Member
Posts: 232
Reputation Power: 72
Ori wrote
Ryzen wrote
Ori wrote
Ori wrote
Ryzen wrote
while(true) {
   $i = rand(1, 10);
   if($i != 5) {
      echo sqrt($i);
   }
}

This would be how I'd do it.


There is a syntax error on that code for me bro. Around the rand function.


Okay I fixed the syntax, however with your code, the web browser is very slow because of the loop. Is there any way to make it hit 5 quicker to terminate?

OH I miss understood what you were asking for haha. One second I'll fxi my code up.

Edit:
This would work
<?php
$a = true;
while($a == true) {
   $b = rand(1, 10);
   if($b != 5) {
      echo sqrt($b) .'<br/>';
   }
   else {
      $a = false;
   }
}
?>


Damn thanks bro. Im pretty sure the code is fixed now. You're a genius haha. How good are you with PHP?

No problem at all.

As for how good I am? I've been doing it for a few years and have picked up a few tricks here and there. I'm not gonna say I'm the best, but I definitely know how to do ALMOST anything.
#9. Posted:
Orry
  • Ladder Climber
Status: Offline
Joined: Mar 01, 201113Year Member
Posts: 358
Reputation Power: 24
Status: Offline
Joined: Mar 01, 201113Year Member
Posts: 358
Reputation Power: 24
Ryzen wrote
Ori wrote
Ryzen wrote
Ori wrote
Ori wrote
Ryzen wrote
while(true) {
   $i = rand(1, 10);
   if($i != 5) {
      echo sqrt($i);
   }
}

This would be how I'd do it.


There is a syntax error on that code for me bro. Around the rand function.


Okay I fixed the syntax, however with your code, the web browser is very slow because of the loop. Is there any way to make it hit 5 quicker to terminate?

OH I miss understood what you were asking for haha. One second I'll fxi my code up.

Edit:
This would work
<?php
$a = true;
while($a == true) {
   $b = rand(1, 10);
   if($b != 5) {
      echo sqrt($b) .'<br/>';
   }
   else {
      $a = false;
   }
}
?>


Damn thanks bro. Im pretty sure the code is fixed now. You're a genius haha. How good are you with PHP?

No problem at all.

As for how good I am? I've been doing it for a few years and have picked up a few tricks here and there. I'm not gonna say I'm the best, but I definitely know how to do ALMOST anything.


Haha nice to know my friend. Is it cool if I add you cos i'm going through some PHP now and theres some code I have that ive been unsure about if they are correct and I was wondering if you could help me out? Ill even repay you after.. nothing too sexual
#10. Posted:
CriticaI
  • Shoutbox Hero
Status: Offline
Joined: Nov 05, 201310Year Member
Posts: 2,737
Reputation Power: 448
Status: Offline
Joined: Nov 05, 201310Year Member
Posts: 2,737
Reputation Power: 448
This will run in an "infinite" loop until the random number is 5.
little bit shorter than ryzen's

$not5 = true;
while($not5){
    $i = rand(1,10);
    if ($i == 5) $not5 = false;
    echo sqrt($i);
}
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.