frame

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In Register

PHP While and For Loops

PHP While and For Loops

The while loop

<!DOCTYPE html>
<html>
<body>


<?php  
$x = 1;
 
while($x <= 5) {
  echo "The number is: $x <br>";
  $x++;
} 
?>  


</body>
</html>

The do...while loop

<!DOCTYPE html>
<html>
<body>


<?php 
$x = 1;


do {
  echo "The number is: $x <br>";
  $x++;
} while ($x <= 5);
?>


</body>
</html>

Another do...while loop

<!DOCTYPE html>
<html>
<body>


<?php 
$x = 6;


do {
  echo "The number is: $x <br>";
  $x++;
} while ($x <= 5);
?>


</body>
</html>

The for loop

<!DOCTYPE html>
<html>
<body>


<?php  
for ($x = 0; $x <= 10; $x++) {
  echo "The number is: $x <br>";
}
?>  


</body>
</html>

The foreach loop

<!DOCTYPE html>
<html>
<body>


<?php  
$colors = array("red", "green", "blue", "yellow"); 


foreach ($colors as $value) {
  echo "$value <br>";
}
?>  


</body>
</html>

The break statement in a loop

<!DOCTYPE html>
<html>
<body>


<?php  
for ($x = 0; $x < 10; $x++) {
  if ($x == 4) {
    break;
  }
  echo "The number is: $x <br>";
}
?>


</body>
</html>

The continue statement in a loop

<!DOCTYPE html>
<html>
<body>


<?php  
for ($x = 0; $x < 10; $x++) {
  if ($x == 4) {
    continue;
  }
  echo "The number is: $x <br>";
}
?>


</body>
</html>
Tagged:
Sign In or Register to comment.

Developer

| make vanilla clean and light
@ 2016 Your Sitename, All rights reserved. Material Design is a design language developed by Google. Material Design makes more liberal use of grid-based layouts and responsive animations and transitions.
Powered by VanillaForums, Designed by ThemeSteam

Contact us

info@example.com
support@example.com
(800) 3032120

Get In Touch