PHP For Loops


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











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: C

    Only Bale is printed as $g++ is done before printing and then checked.


  1. What will be the output of the following PHP code ?
    <?php
    $emp = array("Neha", "Sumi", "Abhay", "Krishna");
    for ($z = 0; $z < count($emp); $z)
    {
    if ($emp[$z++] == "Abhay")
    continue;
    printf ($emp[$z]);
    }
    ?>











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: C

    z is incremented only inside loop z the if condition.



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











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: C

    Loop condition is true as g%(g+1) is a float non zero value in php.


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











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: E

    The c value is changed in the inner loop and reaches five, thus does not execute the second outer loop.



  1. What will be the output of the following PHP code ?
    <?php
    $arr = array("Hello", "Interview", "Mania");
    foreach ($arr as $val)
    {
    if (count($arr) == 2)
    print $val;
    }
    ?>











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: D

    As count($arr) returns 3 the condition is always false.