codescracker


php

PHP Loops



« Previous Tutorial Next Tutorial »


Loops in PHP are used to executed the same block of code several number of times as specified. There are following types of loops available in PHP:

PHP for Loop

PHP for loop is used to execute a block of code, specified number of times. Here is the general form to use for loop in PHP:

for(initialization-counter; condition-checker; incrementation)
{
    code to be executed here;
}

Here is the explanation of the above parameters of the for loop in PHP:

PHP for Loop Example

Here is an example, uses for loop in PHP. This example displayes the numbers from 0 to 10 and the table of 2.

<!DOCTYPE html>
<html>
<body>

<?php

echo "<h1>Printing number from 1 to 10</h1>"."<br/>";

for($x = 0; $x <= 10; $x++)
{
   echo "$x ";
}

echo "<h1>Printing table of 2</h1>"."<br/>";
for($x = 2; $x <= 20; $x=$x+2)
{
   echo "$x ";
}
?>

</body>
</html>

Here is the output produced by the above PHP script:

PHP for Loop

PHP while loop

PHP while loop is used to execute a block of code if and as long as the expression given evaluates to true. Here is the general form to use while loop in PHP:

while(condition)
{
	code to be executed here, if the condition evaluates to true;
	incrementation;
}

PHP while Loop Example

Here is an example, uses while loop in PHP. This example decrements a variable value on each iteration of the while loop and the counter increments until it reaches to 0 when the evaluation is false and the loop goes to end

<html>
<body>

<?php
	$i = 25;
	while($i > 0)
	{
		$i--;
		echo "$i ";
	}
	echo "Fire!!!"."<br/>";
	echo ("Loop stopped at i = $i");
?>

</body>
</html>

It will display the following result:

PHP while loop

PHP do-while Loop

PHP do-while loop is used to execute a block of code at least once. The do-while loop in PHP, is similar to the PHP while loop, but in case of do-while loop, the condition-checker-expression is evaluated at the bottom of the loop. Here is the general form of the do-while loop in PHP:

do
{
   code to be executed here;
}while (condition);

PHP do-while Loop Example

Here is an example used do-while loop in PHP. This example will decrement the value of the variable i at least once, and it will continue decrementing the value of the variable i until its value become equal to 0

<html>
<body>
<?php
$i = 25;

	do
	{
		$i--;
		echo "$i ";
		
	}while($i > 0);
	echo "Fire!!!"."<br/>";
	echo ("Loop stopped at i = $i");
?>
</body>
</html>

Here is the output of the above PHP script:

PHP do...while loop

PHP foreach Loop

The foreach loop only works on arrays, used to loop through each key/value pair in an array. Here is the general form to use foreach loop in PHP:

foreach ($array as $value)
{
    code to be executed here;
}

For every loop iteration, the current array element's value is assigned to the variable $value and then the array pointer is moved by one, until it reaches to the last element of the array. Here is another example, also uses foreach loop in PHP:

<html>
<body>
<?php
	$googlers = array("Alok", "Gaurav", "Anish", "Saurabh");
	foreach ($googlers as $value)
	{
		echo "$value <br>";
	}
?>
</body>
</html>

The above PHP script will produce following output:

PHP foreach loop

PHP break Keyword

PHP break keyword is used to terminate the execution of the loop immediately. Here is an example of break keyword in PHP:

<html>
<body>

<?php
	$i = 10;
	while($i > 0)
	{
		$i--;
		echo "$i ";
		if($i == 5)
			break;
	}
?>
</body>
</html>

In the above PHP script, the loop will conditnue executing until (before) i reached to 5, and then the condition of the if statement becomes true, and break statement executes to terminate the loop immediately. Here is the output of the above PHP script:

PHP break keyword

PHP continue keyword

PHP continue keyword is used to skip the remaining part of the code and program control goes to the condition-checker part of the loop. Here is an example, uses continue keyword in PHP:

<html>
<body>

<?php
	$i = 10;
	while($i > 0)
	{
		$i--;
		if($i == 5)
		continue;
			echo "$i ";
	}
?>
</body>
</html>

Here is the output produced by the above PHP script:

PHP continue keyword

« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test