PHP While Loops


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











  1. View Hint View Answer Discuss in Forum

    NA

    Correct Option: E

    The loop never ends as m is always incremented and then decremented.


  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.



  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
    $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
    $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.