for declaración en un ciclo while no funciona? [cerrado]
Frecuentes
Visto 174 equipos
-1
<!DOCTYPE html>
<html>
<head>
<title>A loop of your own</title>
<link type='text/css' rel='stylesheet' href='style.css'/>
</head>
<body>
<?php
//Add while loop below
$hello = true;
while($hello = true):
{
echo "Loop is runnin";
for($i = 0; $i <= 3; $i++)
{
$hello = false;
}
}
endwhile
?>
</body>
</html>
My error is: infinite loop.
How can I fix this? I'm practising;-)
I want a for loop within a while loop, code must say 3 times "Loop is running" then make the
variable $hello false.
$hello = true;
while($hello == true):
echo "Loop is runnin";
for($i = 0; $i < 3; $i++)
{
$hello = false;
}
endwhile;
This is the result, but if you guys had a sharp eye you would have saw it, the for loop wil only run one time:p Because it will try to set $hello 3x times to false.
But thank you all for answering my question;)
3 Respuestas
3
Intenta hacer así:
while($hello == true)
{
echo "Loop is runnin";
for($i = 0; $i <= 3; $i++)
{
$hello = false;
}
}
endwhile;
Tienes que usar ==
en lugar de =
for comparison as well as adding ;
despues de ti endwhile
If you only want to echo Loop is running
3 times, you need to change:
for($i = 0; $i <= 3; $i++)
a:
for($i = 0; $i < 3; $i++)
o bien:
for($i = 1; $i <= 3; $i++)
Respondido 12 Feb 14, 08:02
Spot the difference...? - deceze
@deceze == instead of = - sashkello
Why not point that out specifically? - deceze
@deceze and the ;
después de endwhile
- Felix
btw its still not the desired result of the first post. he want get 3x loop is running. so in real the echo should be inside the second loop. sense or nonsense.... whatever :) *edit -> $i should also start at 1 for 3x output... now its looping 4 times - MAQU
1
Solo haz esto.
$hello = 0;
while($hello < 3):
echo "Loop is runnin";
$hello++;
endwhile;
Respondido 12 Feb 14, 08:02
This is not exactly what I wanted, but it works great. Thank you! - STP38
0
Asegúrate de leer Manual de PHP for syntax, you need a ';' after your endwhile. Because you've use a colon after your while statement, you shouldn't enclose the while loop code within curly brackets. Try;
$hello = true;
while($hello = true):
echo "Loop is runnin";
for($i = 0; $i <= 3; $i++)
{
$hello = false;
}
endwhile;
Respondido 12 Feb 14, 08:02
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas php if-statement for-loop while-loop or haz tu propia pregunta.
1. Que es
:
doing after while statement? 2. $hello == true is logical expression, $hello = true is assignment. You need the first. - sashkello@sashkello It's starting an alternative while syntax. - deceze
@deceze in that case what are those curly braces doing? ;) - PeeHaa
@deceze It should be either or :) - sashkello
@PeeHaa Not much, but they're not in the way either. It's just a superfluous group. - deceze