PHP For Loops


  1. What will be the output of the following PHP code ?
    <?php
    $s = 2;
    for (1; $s == 1; $s = 2)
    {
    print "In for loop statement executed...";
    }
    print "After for loop statement executed...\n";
    ?>











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: A

    The loop never exits as the condition ++s == s is always satisfied,evaluated from right to left.


  1. What will be the output of the following PHP code ?
    <?php
    for ($n = 2; $n < 5; $n++)
    {
    for ($m = 2; $m < 3; $m++)
    {
    print "Interview Mania \n";
    }
    }
    ?>











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: C

    3*1 times is printed.



  1. What will be the output of the following PHP code ?
    <?php
    for ($number = 1; $number != 20; $number++)
    {
    print $number;
    $number++;
    }
    ?>











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: B

    Condition always fails as number takes only odd numbers.


  1. What will be the output of the following PHP code ?
    <?php
    for ($n = 10; $n < 45; $n++);
    print $n;
    ?>











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: C

    The for loop has no body,it just runs till condition is satisfied.



  1. What will be the output of the following PHP code ?
    <?php
    for ($t = 2; $t < 7; $t++)
    {
    print "Interview\n";
    continue;
    print "Mania";
    }
    ?>











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: B

    When continue is encountered it skips to the next iteration.