PHP Example HTML Javascript jQuery Ajax CSS Java MORE

PHP while loops

In PHP while loops is used to execute a block of code while the specified condition is true.

Some time we want the same block of code to run over and over again.

Example: Print 1 to 10 number.

Example: Fetch multiple students data.In that case we can use while loop.

Syntax

while (condition is true) {
	code to be executed;
}

Example(Print 1 to 10)

<?php
$a=1;
while ($a<=10)
{
echo $a;
$a++;
}
?>

Run It Yourself »

Example(Print all even and odd number between 0 to 10)

<?php
$i=0;
	while($i <= 10){
		if($i % 2 == 0){
			echo $i." - Even,  ";
		}else{
			echo $i." - Odd,  ";
		}
		$i++;
	}
?>

Run It Yourself »