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:
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?
Last edited by Orry ; edited 1 time in total
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:
Status: Offline
Joined: May 16, 20177Year Member
Posts: 232
Reputation Power: 72
Status: Offline
Joined: May 16, 20177Year 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.
- 0useful
- 0not useful
#3. Posted:
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 wrotewhile(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.
- 0useful
- 0not useful
#4. Posted:
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 wroteRyzen wrotewhile(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?
- 0useful
- 0not useful
#5. Posted:
Status: Offline
Joined: May 16, 20177Year Member
Posts: 232
Reputation Power: 72
Status: Offline
Joined: May 16, 20177Year Member
Posts: 232
Reputation Power: 72
#6. Posted:
Status: Offline
Joined: May 16, 20177Year Member
Posts: 232
Reputation Power: 72
Status: Offline
Joined: May 16, 20177Year Member
Posts: 232
Reputation Power: 72
Ori wroteOri wroteRyzen wrotewhile(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;
}
}
?>
- 1useful
- 0not useful
#7. Posted:
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 wroteOri wroteOri wroteRyzen wrotewhile(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?
- 1useful
- 0not useful
#8. Posted:
Status: Offline
Joined: May 16, 20177Year Member
Posts: 232
Reputation Power: 72
Status: Offline
Joined: May 16, 20177Year Member
Posts: 232
Reputation Power: 72
Ori wroteRyzen wroteOri wroteOri wroteRyzen wrotewhile(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.
- 0useful
- 0not useful
#9. Posted:
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 wroteOri wroteRyzen wroteOri wroteOri wroteRyzen wrotewhile(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
- 0useful
- 0not useful
#10. Posted:
Status: Offline
Joined: Nov 05, 201311Year Member
Posts: 2,753
Reputation Power: 452
Status: Offline
Joined: Nov 05, 201311Year Member
Posts: 2,753
Reputation Power: 452
This will run in an "infinite" loop until the random number is 5.
little bit shorter than ryzen's
little bit shorter than ryzen's
$not5 = true;
while($not5){
$i = rand(1,10);
if ($i == 5) $not5 = false;
echo sqrt($i);
}
- 2useful
- 0not useful
You are viewing our Forum Archives. To view or take place in current topics click here.