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
    $s = 2;
    for (1; $s == 1; $s = 2)
    {
    print "In for loop executed...";
    }
    print "After for loop statement executed...\n";
    ?>











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: C

    The loop does not run as s initialized in check statement will be zero.



  1. What will be the output of the following PHP code ?
    <?php
    $k = 1;
    for ($k++; $k == 1; $k = 2)
    print "In for loop execute...";
    print "After loop executed...\n";

    ?>











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: C

    After loop executed...


  1. What will be the output of the following PHP code ?
    <?php
    $Country = array("INDIA","USA","ENGLAND");
    foreach ($Country as $val)
    {
    echo "$val \n";
    }
    ?>











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: D

    This runs a for loop for that array.



  1. What will be the output of the following PHP code ?
    <?php
    for ($num = 1; $num <= 5; $num++)
    {
    echo "The number is: $num \n";
    }
    ?>











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: E

    This runs a for loop from 1 to 5.