PHP For Loops


  1. What will be the output of the following PHP code ?
    <?php
    for ($d = 0; -7 ; $d++)
    {
    print"d";
    if ($d == 4)
    break;
    }
    ?>











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: E

    The break statement after breaks the loop after d = 4,does not print anymore.


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











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: C

    The condition of the loop is always false 0.



  1. What will be the output of the following PHP code ?
    <?php
    for ($v = 0; $v < 5; $v++)
    {
    for ($u = $v; $u > 0; $v--)
    print $v;
    }
    ?>











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: D

    In the second loop u value is not being changed.


  1. What will be the output of the following PHP code ?
    <?php
    for($g = 0; $g < 15; $g++)
    {
    for($h = $g; $h > $g; $g--)
    print $g;
    }
    ?>











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: A

    The second loop does not execute as the check condition is always false.



  1. What will be the output of the following PHP code ?
    <?php
    $emp = array("Ajit", "Ats", "Aju","Rahul");
    for ($p = 0; $p < count($emp); $p++)
    {
    if ($emp[$p] == "Rahul")
    continue;
    printf ($emp[$p]);
    }
    ?>











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: E

    Only the Shrek is skipped due to the continue statement.