Add



PHP Loops

There are four kinds of loops available in PHP language.

1. PHP For Loop

When we already know that for how many times loop needs to be executed then we use for loop. For example,

    for ($idx=0; $idx<10; $idx++)
    {
        // block of code to be executed;
    }

In this example, we know that this block of statements should be executed 10 times that is why for loop has been used.

2. PHP Foreach Loop

When an array is to be manipulated then foreach loop is used.

    foreach ($array as $value)
    {
        // block of code to be executed;
    }

3. PHP While Loop

In while loop, it keeps on executing repeatedly if condition remains true.

    while (condition)
    {
        // block of code to be executed;
    }

4. PHP Do-While Loop

In do-while loop, it is atleast executed once then it keeps on executing repeatedly if while-condition remains true.

    do
    {

        // block of code to be executed;

    }while (condition)