PHPGurukul

Looping Statement In PHP

Loops execute a block of code a specified number of time, or while a specified condition is true.
In PHP , the following looping statements are used

  • The while Loop
  • The Do …while Loop
  • The For Loop
  • The Foreach Loop
  • Break and Continue Statement
  • The While Loop
    While structure is the another type of loop statement, where the condition is checked at first, the iteration will not stop even if the value changes while executing Statements.
    Syntax-

    while(condition)
    {
    code to be executed
    }

    Example- 

    <?php
    $i=0;
    while($i<=10)
    { //output value 0 from 10
    echo "The Number is ".$i."<br>";
    $i++;
    }
    ?>

    Output-
    The Number is 0
    The Number is 1
    The Number is 2
    The Number is 3
    The Number is 4
    The Number is 5
    The Number is 6
    The Number is 7
    The Number is 8
    The Number is 9
    The Number is 10
    The Do While Loop
    Do while statement is same as the while statement, the only difference is that it evaluates the expression at the end.
    Syntax – 

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

    Example –

    <?php
    $i=0;
    do
    { //output value 0 from 10
    echo "The Number is ".$i."<br>";
    $i++;
    }
    while($i<=10)
    ?>

    Output-
    The Number is 0
    The Number is 1
    The Number is 2
    The Number is 3
    The Number is 4
    The Number is 5
    The Number is 6
    The Number is 7
    The Number is 8
    The Number is 9
    The Number is 10
    The For Loop
    The for loop is used when you know in advance how many times the script should run.
    Syntax-

    for(initialization;condition;increment)
    {
    code to be executed
    }

    The for loop statement take three expressions inside its parenthesis. Separated by semi-colons. When the for loop executes, the following occurs –

    • The initialization expression is executed. This expression usually initializes one or more loop counter, but the syntax allows an expression of any degree of complexity.
    • The Condition expression is evaluated. If the value of condition is true, the loop statement execute. If the value of condition is false, the for loop terminates.
    • The update expression increment executes.
    • The statement executes, and control return to step 2.

    Example –

    <?php
    for($i=0;$i<=10;$i++)
    {
    echo "The Number is ".$i."<br/>";
    }
    ?>

    Output-
    The Number is 0
    The Number is 1
    The Number is 2
    The Number is 3
    The Number is 4
    The Number is 5
    The Number is 6
    The Number is 7
    The Number is 8
    The Number is 9
    The Number is 10
    The Foreach Loop
    For Each structure is a loop structure used for arrays.
    Syntax –

    foreach(array as value)
    {
    code to be executed;
    }
    foreach(array as key => value)
    {
    code to be executed;
    }

    Example-

    <?php
    $email=array('info@phpgurukul.com','anuj.lpu1@gmail.com');
    foreach($email as $value)
    {
    echo "Processing ".$value."<br/>";
    }
    ?>

    Output-
    Processing info@phpgurukul.com
    Processing anuj.lpu1@gmail.com
    The Break Statement
    Break end the execution of the for , for each , do-while or switch statement.
    Syntax-

    break(optional numeric argument)

    Example- 

    <?php
    $c=0;
    while(++$c)
    {
    switch($c)
    {
    case 5 :
    echo "At 5;<br />";
    break 1;
    case 10 ;
    echo "At 10;quitting<br />";
    break 2;
    default :
    break;
    }
    }
    ?>

    Output- 
    At 5;
    At 10;quitting
    The Continue Statement
    “Continue” is used to skip the current loop iteration and continue with the next iteration of the loop. But “Break” is used to exist from the whole loop.
    Syntax –

    continue(optional numeric argument);
    

    Example –

    <?php
    for($c=0;$c<=10;++$c)
    {
    if($c==5)
    {
    continue;
    }
    print "$c n";
    }
    ?>

    Output – 0 1 2 3 4 6 7 8 9 10

    Anuj Kumar

    This is Anuj Kumar. I’m a professional web developer with 4+ year experience. I write blogs in my free time. I love to learn new technologies and share with others.
    I founded PHPGurukul in September 2015. The main aim of this website to provide PHP, Jquery, MySQL, PHP Oops and other web development tutorials.
    .

    Recommended Tutorials for you


    Add comment

    This site uses Akismet to reduce spam. Learn how your comment data is processed.

    Web Hosting with a FREE domain. On sale ₹99.00/mo

    Follow us

    Don't be shy, get in touch. We love meeting interesting people and making new friends.