PHP While Loops


  1. What will be the output of the following PHP code ?
    <?php
    $m = 8;
    while (--$m > 0 || ++$m)
    {
    print $m;
    }
    ?>











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: B

    As it is || operator the second expression is not evaluated till m becomes 1 then it goes into a loop.


  1. What will be the output of the following PHP code ?
    <?php
    $n = 1;
    while(++$n || --$n)
    {
    print $n;
    }
    ?>











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: A

    As it is || operator the second expression is not evaluated and n is always incremented, in the first case to 1.



  1. What will be the output of the following PHP code ?
    <?php
    $num = 0;
    while (++$num && --$num)
    {
    print $num;
    }
    ?>











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: C

    The first condition itself fails thus the loop exists.


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











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: B

    (–$t > ++$t) evaluates to 0 but -1 makes it enters the loop and prints t.



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











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: C

    The loop ends when number becomes 0.