Saturday, 15 December 2012

For loop in PHP

For loop in PHP

for loops are the most complex loops in PHP. They behave like their Java and C Language counterparts. The syntax of a For Loop is given below.

For loop Syntax:
for (expr1; expr2; expr3)
statement

The first expression (expr1) is evaluated (executed) once unconditionally at the beginning of the loop. In the beginning of each iteration, expr2 is evaluated. If it evaluates to TRUE, the loop continues and the nested statement(s) are executed. If it evaluates to FALSE, the execution of the loop ends. At the end of each iteration, expr3 is evaluated (executed).

The simple program that prints 0 to 9 numeric values on the screen one by one using php for loop. The sample code will be given below.

<body >
Print 0 to 9 numeric values use for loop<br>
<?php
for ($count = 0; $count < 10; $count = $count + 1) {
echo $count.'<br>';
}
?>
</body>

0 comments:

Post a Comment