Pages

PHP For Loop

In every programming languages loops are applied where the repeated execution of a particular code is required until condition to be tested remains true. PHP For loop is identical to the for loop in C.

for (expression1;expression2;expression3)
{
      //code to be executed
}


The basic syntax of a PHP for loop is shown above.Let's discuss it in detail...
Expression1 is executed unconditionally only once when the loop starts, normally we initialize a variable in expression1.
Expression2 generally contains a condition based on its true or false, the loop decides whether to continue or stop.Expression2 is evaluated on the beginning of each iteration.The PHP for loop becomes indefinite if the expression2 is empty.
Expression3 is executed at the end of each iteration.

Example:

for ($i=1 ; $i<=10 ; $i++)
{
echo "i is equal to ".$i."</br>";
}
Look at the code above,in expression1 the variable is initialized,in expression2
This will display some thing like...
i is equal to 1
i is equal to 2
i is equal to 3
i is equal to 4
i is equal to 5
i is equal to 6
i is equal to 7
i is equal to 8
i is equal to 9
i is equal to 10

 

0 comments:

Post a Comment